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/