Reputation: 67
I want to find a document in a collection by its mongo "_id"
I am trying the following and not getting any result:
var collection = db.get('roadlist');
// Create a new ObjectID
var objectId = ObjectId(req.body._id);
collection.find(
{
"_id": objectId
},
{
},
function(e,docs) {
console.log(e);
res.json(docs);
});
Although I am getting a result by doing mongo shell find operation. I couldn’t find any solution in the previous similar questions.
Upvotes: 1
Views: 2460
Reputation: 22553
It looks like you are mixed up about the parameters the find method takes. It does not appear to take a callback directly, but has, instead, a toArray() method that accepts the callback:
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#find
So to get a result, do something like this:
collection.find(
{"_id": objectId}).toArray(function(err, docs) {
console.log(e);
res.json(docs);
});
Upvotes: 1