Mongo JSON文档-> JSON-> BSON [英] Mongo JSON document -> JSON -> BSON

查看:76
本文介绍了Mongo JSON文档-> JSON-> BSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与Node.js一起构建使用mongodb的Web套接字服务器。

I am working with Node.js to build a web socket server that uses mongodb.

我使用node-mongodb-native作为访问mongo db的库。

I am using node-mongodb-native as the library to access mongo db.

当我调用控制台时.log(sys.inspect(item))从数据库的对象上,我得到的东西看起来像这样:

When I call console.log(sys.inspect(item)) on an object from the db I get something that looks like this:

{ _id: { id: 'L?#&\u008e\u00ad\u000e\u008f\u0014\u0005\u0000\u0000' }
, y: 3
, favorite_color: 'orange'
, x: 14766
}

所以我猜该ID是mongo使用的BSON对象ID。

so I am guessing the id is the BSON object id that mongo uses.

我需要使用JSON将这个对象发送到客户端Web浏览器,让他们对它做一些事情,然后将其发送回服务器。

I need to send this object to the client web browser using JSON, have them do some stuff to it, and then send it back to the server.

当我JSON.stringify(item)时,我得到的东西看起来像这样:

When I JSON.stringify(item), I get something that looks like this:

{"_id":"4c3f23268ead0e8f14050000","y":3,"favorite_color":"orange","x":14766}

因此,id已变成一些十六进制编码的字符串。如果我将其发送给客户端,并且客户端将其发送回,则现在需要在数据库中对其进行更新。我运行JSON.parse(item)使其成为普通对象,但它仍然看起来像这样:

So the id has been turned into some hex encoded string. If I send it to the client, and the client sends it back, I now need to update it in the db. I run JSON.parse(item) to get it to be a normal object, but it still looks like this:

{ _id: '4c3f23268ead0e8f14050000'
, y: 3
, favorite_color: 'orange'
, x: 14766
}

,该_id不能用于在mongodb中查找。

and that _id can't be used to look up in mongodb.

如何将其转换回可用于在mongo上查找的格式?

How can I convert it back to a format that will be able to be used for lookups on mongo?

-更新-

有趣的是,我可以使用 findOne({_ id:item._id},collection)来获取文档,但是如果我这样做:

Interestingly I can use findOne({_id:item._id}, collection) to get the document, but if I do this:

findOne({_ id:{id:item._id.id}},收藏)

我没有收到结果。我猜mongo _id对象有一些特殊之处。

I don't receive a result. I guess there is something special about the mongo _id object.

两者 {_ id:item._id} {_ id:{id:item._id.id}}
倾销时看起来像这样:

Both {_id:item._id} and {_id:{id : item._id.id}} when dumped out look like this:

{ _id: { id: 'L?#&\u008e\u00ad\u000e\u008f\u0014\u0005\u0000\u0000' } }

-已解决的另一个更新---

--Another update RESOLVED---

集成测试文件中有一些对象ID操纵。

There was some object id manipulation in an integration test file.


objectId = new mongo.ObjectID.createFromHexString(’47cc67093475061e3d95369d’);
将提供我要查找的_id。

objectId = new mongo.ObjectID.createFromHexString('47cc67093475061e3d95369d'); will give the _id that I am looking for.

objectId.toHexString()
将返回类似于'47cc67093475061e3d95369d'的十六进制字符串/ p>

objectId.toHexString() will return the hex string that looks like '47cc67093475061e3d95369d'

推荐答案

我的猜测是 sys.inspect 解释了 ObjectId 作为包含 id 的对象属性。这就是您在转储中看到的。

My guess is that sys.inspect interprets an ObjectId as an object containing an id property. That's what you're seeing in the dump.

MongoDB将ObjectId视为12字节的二进制值,而不是对象。因此,MongoDB不知道任何 id 属性。这就是为什么以下查询没有结果的原因:

MongoDB treats the ObjectId as a 12-byte binary value, not as an object. So MongoDB doesn't know about any id property. That's why the following query yields no result:

findOne({_id: {id: item._id.id}}, collection)

下面的方法确实有效,因为它将两个值都视为二进制值:

The following does work, as it just treats both values as binary values:

findOne({_id: item._id}, collection)

这篇关于Mongo JSON文档-> JSON-> BSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆