gpt4 book ai didi

node.js - 如何访问 mongoose.js 中的 oplog?

转载 作者:可可西里 更新时间:2023-11-01 09:20:58 25 4
gpt4 key购买 nike

我的应用程序正在使用 mongoose.js。到目前为止,我一直在使用 mongod 的单个实例。不过最近改成replica set了。

如何在 mongoose.js 中访问 oplog 集合?我的意思是如何从另一个数据库(local)获取集合?假设我已连接到数据库并拥有 mongoose.connection - 我现在如何查询 oplog?

最佳答案

您可以使用 mongoose.connect 方法访问 mongoose 中的另一个数据库,使用 MongoDB URL 指定数据库。例如,要连接到找到 oplog 的“本地”数据库:

mongoose.connect('mongodb://localhost/local')

你说你有兴趣这样做是为了观察 oplog 中的新条目,所以一旦连接到“本地”数据库,你可能希望在“oplog.rs”集合上使用可尾游标成立,例如:

mongoose.connection.once('open', function callback () {
var collection = mongoose.connection.db.collection('oplog.rs')
collection.find({}, {tailable: true}).each(function(err, entry) {
if (err) {
// handle error
} else {
// got a new oplog entry
console.log('--- entry', entry)
}
})
});

希望这对您有所帮助,如果您还有其他问题,请告诉我。

关于node.js - 如何访问 mongoose.js 中的 oplog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18872844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com