node.js - MongoDB _id is convert to ObjectID automatically, but sometime it is not. -
I am using a ring that is called to access mongoDB. Mongoscin is a simple wrapper around the Mongo DB Javascript API.
But when I write to mongodibi, sometimes _id is converted to object id, for some time it is not so. Due to different behavior causes many problems when I have to compare _id. For example:
In the company archive, the following document, "Manufacturer" has not been converted to ObjectID, but in "Client" the item is automatically converted into an object.
& gt; ("53d4b452f5b25900005cb999"), ObjectId ("53d4b452f5b25900005cb99a")]} "" {"_ Id": ObjectId ("53d4b452f5b25900005cb999"), "name": "Customer company for 777 - update", "manufacturer": "53d4b452f5b25900005cb998", "SSN": "12-123-1234"}
This is the nodejose code that I specified _id for the "creator"
clientCompany.creator = req.session.user.company_id;
This is NodesJS code that I specified _id for "client"
var updateObj = {$ addToSet: {clients: resultClient._id} }; // update creator company.clients creatorCompany.update (updateObj, function (error, result) {...}
When I call console.log "req.session.user.company_id" and "Resultclient._id", they both look like a string type. How does a MongoDB end in ObjectID form? If there is an automatic conversion, how can I treat this behavior?
Thank you!
I'm feeling resultClient
the result of a query And req .session.user.company_id
from your web application In case of tring, you need to create an ObjectId from the string:
clientCompany.creator = mongoskin.ObjectID (request.ession.user.company_id);
Comments
Post a Comment