简体   繁体   English

MongoDB oplog随机重复

[英]MongoDB oplog repeats randomly

Currently I'm working with MongoDB and the oplog has been essential to my app. 目前,我正在使用MongoDB,而oplog对我的应用程序至关重要。 During development, I noticed that sometimes the oplog contains the same record (change) 3 or 4 times. 在开发过程中,我注意到有时oplog包含3或4次相同的记录(更改)。

After console.logging about every step in the process of updating the database and tracking the oplog, I'm lost. 在console.log记录了更新数据库和跟踪操作日志过程中的每个步骤之后,我迷路了。

oplogPO.on('update', function (data) { console.log(data.o) } oplogPO.on('update',function(data){console.log(data.o)}

Above code displays { '$set': { status: 1000 } } -- sometimes once, but sometimes 3 or 4 times. 上面的代码显示{'$ set':{status:1000}}-有时一次,但有时3或4次。

Did this happen to anyone else? 这件事发生在别人身上吗? Can someone explain why this happens? 有人可以解释为什么会这样吗?

Also this is my first time posting to stackoverflow, so tell me if I did something wrong ;) 这也是我第一次发布到stackoverflow,所以请告诉我是否做错了;)

Fixed! 固定!

The changes in the oplog are used in sockets, so I put the whole oplog in the connection: oplog中的更改用于套接字,因此我将整个oplog放入连接中:

io.on('connection', function(socket){
 oplogPO.on('update', function (data) { console.log(data.o) }
 socket.emit('updateOrders', {data: "send the socket"});
})

This means that every time I connected, it would increment the amount of repeats. 这意味着每次连接时,都会增加重复次数。

Fixed code: 固定代码:

var socketStuff
oplogPO.on('update', function (data) { 
 socketStuff.emit('updateOrders', {data: "send the socket"});
}

io.on('connection', function(socket){
 socketStuff = socket
 socket.emit('updateOrders', {data: "send the socket"});
})

Maybe not the most elegant way, but it works :) 也许不是最优雅的方式,但是它可以工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM