node.js - Mongodb 错误 : The 'cursor' option is required, 除了使用解释参数进行聚合之外 mongodb 3.6

标签 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 有什么办法可以解决这个问题吗?

最佳答案

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

关于node.js - Mongodb 错误 : The 'cursor' option is required, 除了使用解释参数进行聚合之外 mongodb 3.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48680535/

相关文章:

mongodb - 在 mongodb map-reduce 中使用相同的键多次调用 reduce

javascript - mongoose pre hook findOneAndUpdate 修改从另一个集合获取的某些字段

mongodb - elemMatch 结合 Mongoose 中的其他查询字段

javascript - JS : Textfile to JSON

node.js - 在nodejs中在哪里初始化一个新的数据库连接?

node.js - 为什么名称为 'underscore' 或 'lodash' ?

node.js - Node/表达 : How to POST to an external form with data?

mongodb - 从 ASP.NET Core 以字符串形式返回 ObjectID

node.js - Express Post 请求返回 404 状态

javascript - Mongoose 填充条件