3

I am new to mongoDB, so this may be a really stupid question...

I am trying to access a rails mongo session store from scala.

val sessions = MongoConnection("localhost", 27017)("databaseName")("sessions")
val session = sessions.findOneById("1qzyxraa27shwq2qctkon44fl")

If I print the session, it looks like this:

Some({ "data" : <Binary Data> , "_id" : "1qzyxraa27shwq2qctkon44fl" , "updated_at" : { "$date" : "2013-05-09T04:58:21.054Z"} , "created_at" : { "$date" : "2013-05-09T04:58:21.054Z"}})

If I print the updated_at field:

val updatedAt = session.get("updated_at")
Thu May 09 00:58:21 EDT 2013

The field I am interested in is the data field:

val data = session.get("data")

Problem is, I am not sure really what to do with this, I can't convert it to a string or seem to cast it to anything I've tried.

In the db if I find it manually, the field shows up as:

BinData(0,"BAh7BkkiEF9jc3JmX3Rva2VuB......")

And I am able to process that base64 string manually but how do I get something I can process with casbah?

jgrowl
  • 2,117
  • 2
  • 17
  • 23

1 Answers1

3

After lots of googling, I found this issue on the tracker. Even though it says it was fixed in 2.8, it still is only showing "BinaryData" for the mongo-java-driver-2.11.1.

The following call can be made on your data so that it returns the full data as expected:

com.mongodb.util.JSONSerializers.getStrict().serialize(...)

Using jackson's ObjectMapper, I was able to do something like this to get just the binary data field:

val session = sessions.findOneByID("1qzyxraa27shwq2qctkon44fl")
val data = com.mongodb.util.JSONSerializers.getStrict.serialize(session.get.get("data"))
val mapper = new ObjectMapper()
val tree = mapper.readTree(data)
println(tree.get("$binary"))
jgrowl
  • 2,117
  • 2
  • 17
  • 23