views:

334

answers:

1

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

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

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
}

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

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.

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

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

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
}

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

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

--update--

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

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

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

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

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

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

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

+1  A: 

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 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)
Niels van der Rest
Thank you, that makes a lot more sense now. Is there any way to convert that object to a string or a more portable data type, and then convert it back? I need something to send to the client browser that can be sent back to identify the object.
RobKohr
You should send the `4c3f23268ead0e8f14050000` format to the client, as this seems to be the standard textual representation of an ObjectId. In the Mongo shell the [toString()](http://api.mongodb.org/java/current/org/bson/types/ObjectId.html#toString%28%29) method returns such a string. I expect this to be implemented in node-mongodb-native/node.js as well.
Niels van der Rest
Thanks for your help! Something similar was available as part of the library.
RobKohr