Casbah Scala MongoDB Driver - Embedded Objects

I know that objects in MongoDB can contain several levels of data (just JSON objects can). However, the Casbah driver tutorial only covers creating β€œflat” objects with only one data layer. How can I create and work with multi-level objects using Casbah?

+6
source share
1 answer

Its quite intuitive.

Construction:

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

access to internal fields with dotted notation :

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

search for the internal object as a DBObject, so you can perform the same operations with it as with the parent object:

 val b = a.as[DBObject]("a") 
+9
source

Source: https://habr.com/ru/post/914546/


All Articles