I have a method to find a document in my database based on its ObjectID:
console.log('id: ' + id + ' type: ' + typeof id);
collection.findOne({'_id':new ObjectID(id)}, function(error,doc) {
if (error) {
callback(error);
} else {
callback(null, doc);
}
});
When I run it I get the following error:
/myPath/node_modules/monk/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/connection/base.js:245
throw message;
^
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
at new ObjectID (/myPath/node_modules/mongodb/node_modules/bson/lib/bson/objectid.js:38:11)
at /myPath/collectionDriver.js:134:41
This refers to the collection.findOne()
line above.
The console log I have before that call outputs the id as a string of 24 hex characters:
id: "55153a8014829a865bbf700d" type: string
Before this I convert the id from an object to a string using JSON.stringify()
but it appears to work successfully as shown in my console.log.
Running db.myCollection.findOne({_id : ObjectId("55153a8014829a865bbf700d")})
in Robomongo brings back the expected result.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…