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'