我有一个 MongoDB 本地实例,以 --master 启动,我可以在启动日志中看到我创建了一个副本集 oplog(文件:/data/db/local.1)。每次我执行和 insert() 时,似乎 local.1 都会更新。
我正在尝试使用以下代码尾部/流式传输 oplog...
MongoDB.connect('mongodb://localhost:27017/test',(e,db) => {
if ( e ) { return console.log('Connect MongoDB Error',e); }
console.log('Connected MongoDB');
Server.DB = db;
db.collection('oplog.rs').find({}).toArray((e,d) => {
console.log('oplog.rs');
console.log(e);
console.log(d);
})
var updates = db.collection('oplog.rs').find({},{
tailable: true,
awaitData: true,
noCursorTimeout: true,
oplogReplay: true,
numberOfRetries: Number.MAX_VALUE
}).stream();
updates.on('data',(data) => { console.log('data',data); });
updates.on('error',(e) => { console.log('error',e); });
updates.on('end',(end) => { console.log('end',end); });
});
代码在启动时记录以下内容...
oplog.rs
null
[]
但是流没有输出,我将 numberOfRetries 设置为较低的值,但出现错误..
error { MongoError: No more documents in tailed cursor
at Function.MongoError.create (/Users/peter/node_modules/mongodb-core/lib/error.js:31:11)
at nextFunction (/Users/peter/node_modules/mongodb-core/lib/cursor.js:637:50)
at /Users/peter/node_modules/mongodb-core/lib/cursor.js:588:7
at queryCallback (/Users/peter/node_modules/mongodb-core/lib/cursor.js:211:18)
at Callbacks.emit (/Users/peter/node_modules/mongodb-core/lib/topologies/server.js:116:3)
at Connection.messageHandler (/Users/peter/node_modules/mongodb-core/lib/topologies/server.js:282:23)
at Socket.<anonymous> (/Users/peter/node_modules/mongodb-core/lib/connection/connection.js:273:22)
at emitOne (events.js:96:13)
at Socket.emit (events.js:189:7)
at readableAddChunk (_stream_readable.js:176:18)
name: 'MongoError',
message: 'No more documents in tailed cursor',
tailable: true,
awaitData: true }
end undefined
最佳答案
oplog.rs
用于 replica sets 。使用 --master
启动 mongodb,您正在使用长期弃用的 master-slave replication ,它使用 local.oplog.$main
数据库,因此 oplog.rs
中没有任何内容。
您需要使用 --replSet
启动 mongod 才能从副本集 oplog 中受益。 Read more how to deploy and configure replica sets .
关于javascript - MongoDB oplog 流 Node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43468956/