Skip to content

Instantly share code, notes, and snippets.

@bwmcadams
Created May 17, 2011 20:22
Show Gist options
  • Save bwmcadams/977298 to your computer and use it in GitHub Desktop.
Save bwmcadams/977298 to your computer and use it in GitHub Desktop.
Dynamic Casbah Example
import com.mongodb.casbah.Imports._
import com.mongodb.casbah.dynamic._
val d = DynamicDBObject("_id" -> new ObjectId, "name" -> "Brendan McAdams",
"address" -> MongoDBObject("street" -> "134 5th Ave Fl 3",
"city" -> "New York",
"state" -> "NY",
"zip" -> 10011),
"email" -> "[email protected]")
d.name.typed[String]
// res4: Option[String] = Some(Brendan McAdams)
d.name.typed[Int]
// res5: Option[Int] = None
d.address.city.typed[String]
// res6: Option[String] = Some(New York)
d.address.zip.typed[Int]
// res7: Option[Int] = Some(10011)
d.address.zip.typed[String]
// res8: Option[String] = None
d.address.typed[DBObject]
// res9: Option[com.mongodb.casbah.Imports.DBObject] = Some({ "street" : "134 5th Ave Fl 3" , "city" : "New York" , "state" : "NY" , "zip" : 10011})
d.foo.bar.baz.bang.more.stuff.that.dont.exist.typed[String]
// res10: Option[String] = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment