Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  150] [ 6]  / answers: 1 / hits: 38203  / 11 Years ago, tue, july 9, 2013, 12:00:00

I'm using MongoDb (as part of MongoJS) in Node. Here is the documentation for MongoJS.



I'm trying to do a call within Node based on an entry's _id field. When using vanilla MongoDB from the console, I can do:



db.products.find({_id:ObjectId(51d151c6b918a71d170000c7)})



and it correctly returns my entry. However, when I do the same thing in Node, like:



db.products.find({_id: ObjectId(51d151c6b918a71d170000c7)}, function (err, record) {
// Do stuff
});


I get ReferenceError: ObjectId is not defined.



What is the correct protocol for doing this?


More From » node.js

 Answers
22

You need to require the ObjectId function before using it:



var ObjectId = require('mongodb').ObjectID;

[#77121] Sunday, July 7, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
payne

Total Points: 527
Total Questions: 108
Total Answers: 88

Location: Tajikistan
Member since Thu, Apr 14, 2022
2 Years ago
;