Closed CrankyDragon closed 12 years ago
Here's a quick code sample:
var Mongolian = require('mongolian') , db = new Mongolian('mongo://localhost/modelr_db', {log:false}) , collection = db.collection('test_collection') collection.insert({ _id : '4f03ce14000000df01000002', hello : 'World', }) collection.findOne(function(err, doc_1) { var id = doc_1._id.toString(); var query = { _id: new Mongolian.ObjectId(id) } // This don't work console.log(doc_1) collection.findOne(query, function(err, doc_2) { console.log(doc_2) }) /// But this works query = { _id: doc_1._id} collection.findOne(query, function(err, doc_3) { console.log(doc_3) }) })
And the output:
console.log(doc_1) -> { _id: '4f03ce14000000df01000002', hello: 'World' } console.log(doc_2) -> undefined console.log(doc_3) -> { _id: '4f03ce14000000df01000002', hello: 'World' }
Why does the query fail when using a string made into an ObjectId?
$ node --version v0.6.6
$ mongo --version MongoDB shell version: 2.0.1
I'm also using 0.1.15
and nvm... i'm an idiot...
var id = doc_1._id.toString(); var query = { _id: id }
Here's a quick code sample:
And the output:
console.log(doc_1) -> { _id: '4f03ce14000000df01000002', hello: 'World' } console.log(doc_2) -> undefined console.log(doc_3) -> { _id: '4f03ce14000000df01000002', hello: 'World' }
Why does the query fail when using a string made into an ObjectId?