Working with MongoDBObject

Skip to first unread message

Nov 25, 2015, 12:14:57 AM11/25/15
to mongodb-casbah-users

I am having difficulty extracting the value I need from MongoDBObject

Let's take this simple example

val mObj = MongoDBObject( "attributes" -> 
  MongoDBList(MongoDBObject("name"->"owner", "value"->"john")) 

I want to obtain the "value" of the "owner" attribute.

I tried this[Seq[Map[String,Any]]]("attributes")

But, but the runtime types are not correct. The conversion only happens on the first level.

I tried


But, again this converts it to a java Map. Also, the conversion is shallow.

Last resort[MongoDBList]("attributes").as[DBObject](0).as[String]("value")

This worked. But, I am not still not sure how I can do a find on the List to look for the attribute name. Also, I am forced to use as[DBObject] instead of as[MongoDBObject].

We purposefully don't use only JSON data in the db (i.e no ObjectId, DBRef or Date etc). So, one option I am considering is converting the result to Json4s and work with that instead. 

I am open to other suggestions. 

Ole Langbehn

Nov 25, 2015, 3:04:58 AM11/25/15
On 25.11.2015 06:14, wrote:
> MongoDBObject("name"->"owner","value"->"john")

MongoDBObject("owner" -> "john", "age" -> "42")


Reply all
Reply to author
0 new messages