Working with MongoDBObject

14 views
Skip to first unread message

sinchron...@gmail.com

unread,
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

 mObj.as[Seq[Map[String,Any]]]("attributes")

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

I tried

mObj.toMap

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

Last resort

  mObj.as[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

unread,
Nov 25, 2015, 3:04:58 AM11/25/15
to mongodb-ca...@googlegroups.com
On 25.11.2015 06:14, sinchron...@gmail.com wrote:
> MongoDBObject("name"->"owner","value"->"john")

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

HTH



signature.asc
Reply all
Reply to author
Forward
0 new messages