简体   繁体   中英

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?

You should be able to use:

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

No need for the "new" on the beginning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM