简体   繁体   中英

MongoDB oplog repeats randomly

Currently I'm working with MongoDB and the oplog has been essential to my app. During development, I noticed that sometimes the oplog contains the same record (change) 3 or 4 times.

After console.logging about every step in the process of updating the database and tracking the oplog, I'm lost.

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

Above code displays { '$set': { status: 1000 } } -- sometimes once, but sometimes 3 or 4 times.

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 ;)

Fixed!

The changes in the oplog are used in sockets, so I put the whole oplog in the connection:

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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