| ]
| }""").asInstanceOf[DBObject]
doc: com.mongodb.casbah.Imports.DBObject = { "author" : "joe" , "title" : "Yet another blog post" , "text" : "Here is the text..." , "tags" : [ "example" , "joe"] , "comments" : [ { "author" : "jim" , "comment" : "I disagree"} , { "author" : "nancy" , "comment" : "Good post"}]}
scala> doc("created") = new java.util.Date()
scala> doc("created")
res6: AnyRef = Thu Mar 31 16:25:22 EDT 2011
// Save the doc to a collection
scala> val mongo = MongoConnection()("mongodb-user")("scala")
mongo: com.mongodb.casbah.MongoCollection = MongoCollection()
scala> mongo.insert(doc)
res7: com.mongodb.WriteResult = N/A
scala> mongo.findOne()
res8: Option[mongo.T] = Some({ "_id" : { "$oid" : "4d94e366ebac96e4e37e17c8"} , "author" : "joe" , "title" : "Yet another blog post" , "text" : "Here is the text..." , "tags" : [ "example" , "joe"] , "comments" : [ { "author" : "jim" , "comment" : "I disagree"} , { "author" : "nancy" , "comment" : "Good post"}] , "created" : { "$date" : "2011-03-31T20:25:22Z"}})
scala> mongo.head
res0: mongo.T = { "_id" : { "$oid" : "4d94e366ebac96e4e37e17c8"} , "author" : "joe" , "title" : "Yet another blog post" , "text" : "Here is the text..." , "tags" : [ "example" , "joe"] , "comments" : [ { "author" : "jim" , "comment" : "I disagree"} , { "author" : "nancy" , "comment" : "Good post"}] , "created" : { "$date" : "2011-03-31T20:25:22Z"}}
scala> val id = mongo.head.getAs[ObjectId]("_id").get
id: com.mongodb.casbah.Imports.ObjectId = 4d94e366ebac96e4e37e17c8
// You could also just read _id from the doc, as it is added as the doc is inserted
scala> val updateQ = $push ("comments" -> MongoDBObject("author" -> "brendan", "comment" -> "OMG! Ponies!"))
updateQ: com.mongodb.casbah.commons.Imports.DBObject = { "$push" : { "comments" : { "author" : "brendan" , "comment" : "OMG! Ponies!"}}}
scala> mongo.update(MongoDBObject("_id" -> id), updateQ)
res2: com.mongodb.WriteResult = N/A
scala> mongo.head.getAs[MongoDBList]("comments")
res12: Option[com.mongodb.casbah.Imports.MongoDBList] = Some([ { "author" : "jim" , "comment" : "I disagree"} , { "author" : "nancy" , "comment" : "Good post"} , { "author" : "brendan" , "comment" : "OMG! Ponies!"}])
Using the $push syntax will add an additional document onto the end of the Array.