marcello3d / node-mongolian

[project inactive] Mongolian DeadBeef is an awesome Mongo DB driver for node.js
https://groups.google.com/group/node-mongolian
zlib License
349 stars 50 forks source link

Can't find records when searching by the _id as a string #71

Closed CrankyDragon closed 12 years ago

CrankyDragon commented 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?

CrankyDragon commented 12 years ago

$ node --version v0.6.6

$ mongo --version MongoDB shell version: 2.0.1

I'm also using 0.1.15

CrankyDragon commented 12 years ago

and nvm... i'm an idiot...

var id = doc_1._id.toString(); var query = { _id: id }