Mongodb错误:'cursor'选项是必需的,除了带有explain参数mongodb 3.6的聚合

时间:2018-02-08 08:09:18

标签: node.js mongodb mongoose

Mongodb(版本3.6)聚合无法正常工作。以前我使用的是mongodb 3.4和mongoose 4.7.0。

Books.aggregate([{
            $match: filter
        }, {
            $lookup: {
                from: 'users',
                localField: 'user_id',
                foreignField: '_id',
                as: 'user'
            }
        }], function(err, list) {

以上代码与mongodb 3.4一起正常运行。但是在将mongodb更新到3.6后,聚合不起作用。它抛出以下错误

The 'cursor' option is required, except for aggregate with the explain argument

由于许多依赖项,我也无法更新mongoose。那么有没有办法用mongoose 4.7.0解决这个问题?

1 个答案:

答案 0 :(得分:0)

AGGREGATIONCURSOR Cannot directly be instantiated

实施例

const MongoClient = require('mongodb').MongoClient;
const test = require('assert');
// Connection url
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'test';
// Connect using MongoClient
MongoClient.connect(url, function(err, client) {
  // Create a collection we want to drop later
  const col = client.db(dbName).collection('createIndexExample1');
  // Insert a bunch of documents
  col.insert([{a:1, b:1}
    , {a:2, b:2}, {a:3, b:3}
    , {a:4, b:4}], {w:1}, function(err, result) {
    test.equal(null, err);
    // Show that duplicate records got dropped
    col.aggregation({}, {cursor: {}}).toArray(function(err, items) {
      test.equal(null, err);
      test.equal(4, items.length);
      client.close();
    });
  });
});