Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casbah Scala MongoDB driver - embedded objects

I know that objects in MongoDB can contain multiple levels of data (just the way JSON objects can). However, the Casbah driver tutorial only covers the creation of "flat" objects, where there is just one level of data. How can I create and work with multilevel objects with Casbah?

like image 895
noncom Avatar asked Apr 30 '12 14:04

noncom


1 Answers

Its pretty intuitive.

construction:

val a: MongoDBOBject = DBObject("a" -> DBObject("b" -> "c"))
// results in { "a" : { "b" : "c"}}

access to inner fields with dot notation:

val c = a.expand[String]("a.b")

retrieval of inner object as DBObject, so you can make the same operations with it as with parent object:

val b = a.as[DBObject]("a")
like image 192
om-nom-nom Avatar answered Nov 09 '22 09:11

om-nom-nom