【问题标题】:Mongodb error : The 'cursor' option is required, except for aggregate with the explain argument mongodb 3.6Mongodb 错误:'cursor' 选项是必需的,除了带有解释参数 mongodb 3.6 的聚合
【发布时间】:2018-03-18 23:00:14
【问题描述】:

Mongodb(ver 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有没有办法解决这个问题呢?

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    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();
        });
      });
    });
    

    【讨论】:

      猜你喜欢
      • 2019-11-29
      • 2018-10-10
      • 1970-01-01
      • 2018-07-07
      • 2017-10-12
      • 2019-05-09
      • 2014-12-11
      • 2022-10-07
      • 1970-01-01
      相关资源
      最近更新 更多