Search code examples
node.jsmongodbnode-mongodb-native

MongoDB retrieval error


I am retrieving a document from MongoDB using find() in Node.js, while printing the result I am not getting the retrieved values. Here my code..

       db.collection("Product").find({'entry_id':entryID},function(err, result) {
        console.log("Output:",result);

Solution

  • You could just about place money on entry_id is an ObjectId value and what you are passing as a variable is actually just a string.

    But the other obvious thing is that you are using .find() in the wrong way. The "result" returned is a "cursor". If you want something that looks like a whole result set, then use .toArray() or other similar method of conversion:

           var ObjectID = require('mongodb').ObjectID;
    
           db.collection("Product").find({
               'entry_id': new ObjectID(entryID)
           }).toArray(function(err, result) {
            console.log("Output:",result);
           });