Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a callback on MongoDB collection.find()

When I run collection.find() in MongoDB/Node/Express, I'd like to get a callback when it's finished. What's the correct syntax for this?

 function (id,callback) {      var o_id = new BSON.ObjectID(id);      db.open(function(err,db){       db.collection('users',function(err,collection){         collection.find({'_id':o_id},function(err,results){  //What's the correct callback synatax here?           db.close();           callback(results);         }) //find       }) //collection     }); //open   } 
like image 505
John Avatar asked Jul 26 '12 02:07

John


People also ask

What does find () do in MongoDB?

Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents.

What is callback MongoDB?

Callbacks are often used to enforce the order of processing commands. In the MongoDB Node.js driver, you can optionally declare a callback method to async operations that normally return Promises.

What is db collection find ()?

The db. collection. find() method is used to perform a query on a document in a collection and returns a cursor to the selected documents. Syntax: db.collection.find(query, projection)

How retrieve data from collections in MongoDB?

You can use read operations to retrieve data from your MongoDB database. There are multiple types of read operations that access the data in different ways. If you want to request results based on a set of criteria from the existing set of data, you can use a find operation such as the find() or findOne() methods.


2 Answers

That's the correct callback syntax, but what find provides to the callback is a Cursor, not an array of documents. So if you want your callback to provide results as an array of documents, call toArray on the cursor to return them:

collection.find({'_id':o_id}, function(err, cursor){     cursor.toArray(callback);     db.close(); }); 

Note that your function's callback still needs to provide an err parameter so that the caller knows whether the query worked or not.

2.x Driver Update

find now returns the cursor rather than providing it via a callback, so the typical usage can be simplified to:

collection.find({'_id': o_id}).toArray(function(err, results) {...}); 

Or in this case where a single document is expected, it's simpler to use findOne:

collection.findOne({'_id': o_id}, function(err, result) {...}); 
like image 177
JohnnyHK Avatar answered Sep 21 '22 16:09

JohnnyHK


Based on JohnnyHK answer I simply wrapped my calls inside db.open() method and it worked. Thanks @JohnnyHK.

app.get('/answers', function (req, res){      db.open(function(err,db){ // <------everything wrapped inside this function          db.collection('answer', function(err, collection) {              collection.find().toArray(function(err, items) {                  console.log(items);                  res.send(items);              });          });      }); }); 

Hope it is helpful as an example.

like image 42
Anmol Saraf Avatar answered Sep 22 '22 16:09

Anmol Saraf