BulkWrite not committing changes as expected

30 views
Skip to first unread message

Taran Vohra

unread,
Oct 7, 2019, 2:39:22 AM10/7/19
to Mongoose Node.JS ODM
I am running MongoDB 4.0 on MongoDB Atlas Free Tier and using Mongoose 5.7 ODM in Node.js

My problem is the following,

I am performing a "bulkWrite" (10 updates) and what is happening is that those changes happen and then they get automatically rolled back. Or sometimes it doesn't happen at all. Out of 10 for example some of them doesn't update at all. They are simple $inc and $set operations. There is no error thrown, even checked mongodb logs, Why is bulkWrite not committing accurately?

Here is the code,

 let serverId = '96932523957358235';
 
const winningTeam = 1;
 
const changeWinner = false;
 
const name = "fooGame";
 await
Users.bulkWrite(
      players
.map(({ id, team, username }) => {
       
const wonInc = team === winningTeam ? 1 : changeWinner ? -1 : 0;
       
const lostInc = team !== winningTeam ? 1 : changeWinner ? -1 : 0;
       
return {
          updateOne
: {
            filter
: { id, server_id: serverId },
            update
: {
              $set
: { username },
              $inc
: {
               
[`stats.${name}.won`]: wonInc,
               
[`stats.${name}.lost`]: lostInc,
             
},
           
},
         
},
       
};
     
})
   
);

As you can see, players is an array (10 objects) and I'm simply updating stats. What is wrong here that whenever I run this piece then randomly bulkWrite will not commit as expected? 
Reply all
Reply to author
Forward
0 new messages