Nodejs AggregationCursour.each()和rewind()bug

时间:2017-08-07 11:59:32

标签: node.js mongodb each aggregation rewind

我需要在Mongo DB集合上执行复杂的聚合。我还需要两次完成结果。第一次传递后,我在游标上执行rewind()。然后我尝试进行第二次传球。这是我得到错误的地方。如果我用each()几个替换next(),会有什么奇怪的。然后一切都按预期工作。

这是each()中的错误,我应该将其提交给MongoDB bugtracker吗?或者这是我的错误?

许多简化的样本,但仍然重现错误:

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

MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
  // Create a collection
  let collection = db.collection('aggregation_each_example');
  // collection.drop();

  // Some docs for insertion
  function createData(callback) {
    let docs = [
      {
        "oid" : new ObjectId("59883f2e7d8c6325200b81e4"),
        "tod" : new Date("2017-08-07T10:21:34.877Z")
      },  
      {
        "veryImportanta" : "newOne",
        "oid" : new ObjectId("59883f2e7d8c6325200b81e4")
      }  
    ];

    // Insert the docs
    collection.insertMany(docs, callback);
  }

  function getData(callback) {
    return function(err) {
      if (err) {
        console.error(err);
      } else {
        let cursor = collection.aggregate([
                                            {
                                              "$match": {
                                                "oid": new  ObjectId("59883f2e7d8c6325200b81e4"),
                                                "tod": {
                                                  "$exists": 0
                                                }
                                              }
                                            }
                                          ], {cursor: {batchSize: 1}});
        let count = 0;
        cursor.each(function(err, doc) {
          if(doc) {
            console.log(doc);
            count++;
          } else {
            console.log(cursor.isClosed());
            cursor.rewind();
            console.log(cursor.isClosed());
            callback(count, cursor);
          }
        });

      }
    }
  }

  createData(getData(function(count, cursor) {
    console.log("Count: "+ count);
    console.log("Cursor is closed: " + cursor.isClosed());
    cursor.next(function(err, doc) {
      if (err) console.log(err);
      else console.log(doc);
      // db.dropDatabase();
      db.close();
    });
  }));

});

输出:

{ _id: 598851ad48a1841c18b50bcf,
  veryImportanta: 'newOne',
  oid: 59883f2e7d8c6325200b81e4 }
true
false
Count: 1
Cursor is closed: false
{ MongoError: Cursor is closed
    at Function.MongoError.create (error.js:31:11)
    at nextObject (node_modules\mongodb\lib\cursor.js:669:112)
    at AggregationCursor.Cursor.next (node_modules\mongodb\lib\cursor.js:269:12)
    at error.js:61:12
    at error.js:50:13
    at handleCallback (node_modules\mongodb\lib\utils.js:120:56)
    at node_modules\mongodb\lib\cursor.js:748:16
    at handleCallback (node_modules\mongodb\lib\utils.js:120:56)
    at node_modules\mongodb\lib\cursor.js:682:5
    at handleCallback (node_modules\mongodb-core\lib\cursor.js:171:5) name: 'MongoError', message: 'Cursor is closed', driver: true }

环境:

  • nodejs:v6.6.0
  • mongodb:2.2.30
  • os:windows 10
  • mongodb engine:3.4

0 个答案:

没有答案