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?

4

1 回答 1

3

经过大量谷歌搜索,我在跟踪器上发现了这个问题。尽管它说它已在 2.8 中修复,但它仍然只显示 mongo-java-driver-2.11.1 的“BinaryData”。

可以对您的数据进行以下调用,以便它按预期返回完整数据:

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

使用杰克逊的 ObjectMapper,我能够做这样的事情来获取二进制数据字段:

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"))
于 2013-05-11T07:47:43.880 回答