javascriptnode.jsmongodbmongohqdatabase

Finding MongoDB Document by ID


I'm using Node.js and MongoDB with my database hosted on MongoHQ (Now compose.io). I have a general understanding document IDs are converted to hex strings but I can't figure out how to retrieve a document using its ID.

My document has the ID _id: ObjectId("53f13064b5a39cc69f00011b") as in that's how it's displayed in Compose's interface. When I retrieve the document by brute force, the ID is shown as _id: 53f13064b5a39cc69f00011b.

What do I use in Node.js to retrieve this document? The query:

systemData.find({_id: "53f13064b5a39cc69f00011b"}).toArray(function(err, data) {//do stuff}

returns an empty set but so does querying with an object ID object

systemData.find({_id: new ObjectID("53f13064b5a39cc69f00011b")}).toArray(function(err, data) {//do stuff}

What am I missing?


Solution

  • You should be able to use:

    systemData.find({_id: ObjectID("53f13064b5a39cc69f00011b")})
    

    No need for the "new" on the beginning.