我正在运行MongoDB 4.0复制集并从NodeJ访问它,但是在使用聚合时遇到了问题。
当聚合返回的结果数超过默认值或指定的batchSize时,出现以下错误:
MongoError: afterClusterTime is not allowed for this command
at ...\mongodb-core\lib\connection\pool.js:581:63
at authenticateStragglers (...\mongodb-core\lib\connection\pool.js:504:16)
at Connection.messageHandler (...\mongodb-core\lib\connection\pool.js:540:5)
at emitMessageHandler (...\mongodb-core\lib\connection\connection.js:310:10)
at Socket.<anonymous> (...\mongodb-core\lib\connection\connection.js:453:17)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:559:20)
我已将问题缩小到会话的使用范围,这是MongoDB或NodeJS驱动程序的问题,还是在执行此操作时缺少的东西?
作为参考,以下内容足以说明问题。
使用Zero-config MongoDB runner(run-rs --version = 4.0.0)产生的MongoDB 4.0复制集
NodeJs
package.json
{
"name": "aggregate_test",
"dependencies": {
"mongodb": "^3.1.4",
"yargs": "^12.0.5"
}
}
index.js
#!/usr/bin/env node
var args = require('yargs').argv;
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017,localhost:27018,localhost:27019?replicaSet=rs', {useNewUrlParser: true})
.then(function (client) {
var session = args.session !== false ? client.startSession() : undefined;
var db = client.db('test');
var collection = db.collection('test_collection');
return collection.insertMany([
{item: 'one'},
{item: 'two'},
{item: 'three'}
], {session: session})
.then(function () {
return collection.aggregate(
[{$match: {item: {$exists: true}}}],
{session: session, cursor: {batchSize: 1}});
})
.then(function (cursor) {
/* Error occurs as result of this call */
return cursor.toArray();
});
})
.then(function (result) {
console.log(result);
process.exit(0);
})
.catch(function (err) {
console.error(err);
process.exit(1);
});