Get hold of a collection object using something like this:
scala> val collection = MongoClient()("test")("messages")
collection: com.mongodb.casbah.MongoCollection = messages
Where test is the database and messages is the name of the collection.
Inserting arrays is nice and easy, just build up your MongoDBObject with Lists inside:
scala> collection.insert(MongoDBObject("message" -> "Hello World") ++ ("countries" -> List("England","France","Spain")))
res18: com.mongodb.casbah.Imports.WriteResult = { "serverUsed" : "/127.0.0.1:27017" , "n" : 0 , "connectionId" : 234 , "err" : null , "ok" : 1.0}
Use your favourite one liner to print all the objects in the collection:
scala> collection.foreach(println(_))
{ "_id" : { "$oid" : "523f145e30041dae32fd04da"} , "message" : "Hello World" , "countries" : [ "England" , "France" , "Spain"]}
Now lets say you want a list of objects, simply create a list of MongoDBObjects:
scala> collection.insert(MongoDBObject("message" -> "A list of objects?") ++ ("objects" -> List(MongoDBObject("name" -> "England"),MongoDBObject("name" -> "France"))))
res20: com.mongodb.casbah.Imports.WriteResult = { "serverUsed" : "/127.0.0.1:27017" , "n" : 0 , "connectionId" : 234 , "err" : null , "ok" : 1.0}
scala> collection.foreach(println(_))
{ "_id" : { "$oid" : "523f145e30041dae32fd04da"} , "message" : "Hello World" , "countries" : [ "England" , "France" , "Spain"]}
{ "_id" : { "$oid" : "523f14b530041dae32fd04db"} , "message" : "A list of objects?" , "objects" : [ { "name" : "England"} , { "name" : "France"}]}
Now reading them back out of Mongo and processing the array items individually. First we can get a hold
of an object that contains an array:
scala> val anObjectThatContainsAnArrayOfObjects = collection.findOne().get
anObjectThatContainsAnArrayOfObjects: collection.T = { "_id" : { "$oid" : "523f145e30041dae32fd04da"} , "message" : "Hello World" , "countries" : [ "England" , "France" , "Spain"]}
The extra get is on the end as we used the findOne method this time and it returns an Option. Then we
can get just the array field:
val mongoListOfObjects = anObjectThatContainsAnArrayOfObjects.getAs[MongoDBList]("countries").get
mongoListOfObjects: Option[com.mongodb.casbah.Imports.MongoDBList] = Some([ "England" , "France" , "Spain"])
Now we have a handle on a MongoDBList which represents our array in Mongo. The MongoDBList is Iterable
so we can loop through and print it out:
scala> mongoListOfObjects.foreach( country => println(country) )
England
France
Spain
Or map it to a sequence of Strings:
scala> val listOfCountries = mongoListOfObjects.map(_.toString)
listOfCountries: scala.collection.mutable.Seq[String] = ArrayBuffer(England, France, Spain)
scala> listOfCountries
res24: scala.collection.mutable.Seq[String] = ArrayBuffer(England, France, Spain)