如何获得MongoDB集合的回调。find()

时间:2022-05-06 16:48:13

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?

当我在MongoDB/Node/Express中运行collection.find()时,我希望在它完成时得到回调。它的正确语法是什么?

 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
  }

2 个解决方案

#1


39  

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:

这是正确的回调语法,但是find为回调提供的是游标,而不是文档数组。因此,如果您希望回调以文档数组的形式提供结果,请调用光标上的toArray来返回它们:

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.

注意,函数的回调仍然需要提供一个err参数,以便调用者知道查询是否有效。

2.x Driver Update

2。x驱动程序更新

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

find now返回游标,而不是通过回调提供游标,因此通常的用法可以简化为:

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:

或者在这种情况下,如果只需要一个文档,那么使用findOne会更简单:

collection.findOne({'_id': o_id}, function(err, result) {...});

#2


21  

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

基于JohnnyHK的回答,我简单地将调用包装在db.open()方法中,它就可以工作了。谢谢@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.

希望能作为一个例子。

#1


39  

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:

这是正确的回调语法,但是find为回调提供的是游标,而不是文档数组。因此,如果您希望回调以文档数组的形式提供结果,请调用光标上的toArray来返回它们:

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.

注意,函数的回调仍然需要提供一个err参数,以便调用者知道查询是否有效。

2.x Driver Update

2。x驱动程序更新

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

find now返回游标,而不是通过回调提供游标,因此通常的用法可以简化为:

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:

或者在这种情况下,如果只需要一个文档,那么使用findOne会更简单:

collection.findOne({'_id': o_id}, function(err, result) {...});

#2


21  

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

基于JohnnyHK的回答,我简单地将调用包装在db.open()方法中,它就可以工作了。谢谢@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.

希望能作为一个例子。