Alex Rivera | Logout

Querying a MongoDB based on Mongo ID in a node.js app

Asked 2012-10-07T13:28:57.693
16

I'm using a node.js and mongodb, and I'm trying to query the database based on the mongo generated ID using the following:

    collection.findOne( {_id:doc._id} , function(err, item) {});

I am 100% certain that my doc._id is an exact match to the doc _id that I am looking for in the collection, and yet I get a null response from the db query.

I have tried this using other keys in the document and it returns the document just fine. It's only when I try to use the mongo ID.

Edit
Report

1 Answer

17

The MongoDb is an object not a string. To convert my string I used:

    var id = require('mongodb').ObjectID(doc._id);

This converts my string into a mongo ObjectId and matches the _id in the db!

answered 2012-10-07T14:09:30.183

Your Answer