1

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?

Community
  • 1
  • 1
Alex H Hadik
  • 774
  • 2
  • 7
  • 16
  • By brute force I just mean getting all documents in the collection and finding the one I'm interested in and looking at it in the console. – Alex H Hadik Aug 18 '14 at 14:57
  • 1
    You're querying with a _new_ `ObjectID`, hence the problem – Ben Aug 18 '14 at 15:16

1 Answers1

5

You should be able to use:

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

No need for the "new" on the beginning.

vintastic
  • 640
  • 3
  • 7