How to correctly perform bulk update / update in MongoDB

I'm trying to:

  • Find your document according to your search criteria,
  • If found, update some attributes
  • If you do not insert a document with some attributes.

I am using Bulk.unOrderedOperation as I am also doing one insertion. And I want to do everything in one operation again. Db.

However, something that does not cause anything is inserted for the update / restore operation.

This is the insert document:

  var lineUpPointsRoundRecord = { lineupId: lineup.id, // String totalPoints: roundPoints, // Number teamId: lineup.team, // String teamName: home.team.name, // String userId: home.iduser, // String userName: home.user.name, // String round: lineup.matchDate.round, // Number date: new Date() } 

This is an extended document:

  var lineUpPointsGeneralRecord = { teamId: lineup.team, // String teamName: home.team.name, // String userId: home.iduser, // String userName: home.user.name, // String round: 0, signupPoints: home.signupPoints, // String lfPoints: roundPoints+home.signupPoints, // Number roundPoints: [roundPoints] // Number }; 

This is how I try to update / update:

 var batch = collection.initializeUnorderedBulkOp(); batch.insert(lineUpPointsRoundRecord); batch.find({team: lineUpPointsRoundRecord.teamId, round: 0}). upsert(). update({ $setOnInsert: lineUpPointsGeneralRecord, $inc: {lfPoints: roundPoints}, $push: {roundPoints: roundPoints} }); batch.execute(function (err, result) { return cb(err,result); }); 

Why not upgrade / upgrade?

Note

This is JS code using the ORM waterline, which also uses the mongodb native driver.

+5
source share
2 answers

Your syntax here is mostly correct, but your overall execution was incorrect, and you had to β€œseparate” the β€œupsert” action from other changes. Otherwise, they will "collide" and cause an error when "upsert" occurs:

 LineupPointsRecord.native(function (err,collection) { var bulk = collection.initializeOrderedBulkOp(); // Match and update only. Do not attempt upsert bulk.find({ "teamId": lineUpPointsGeneralRecord.teamId, "round": 0 }).updateOne({ "$inc": { "lfPoints": roundPoints }, "$push": { "roundPoints": roundPoints } }); // Attempt upsert with $setOnInsert only bulk.find({ "teamId": lineUpPointsGeneralRecord.teamId, "round": 0 }).upsert().updateOne({ "$setOnInsert": lineUpPointsGeneralRecord }); bulk.execute(function (err,updateResult) { sails.log.debug(err,updateResult); }); }); 

Make sure your sails-mongo is the latest version that supports bulk operations, usually including the recent built-in node driver. The latest one supports the v2 driver, which is suitable for this.

+6
source

Generally, I always set upsert as a property when updating. Also, the update should be able to find the record by itself, so there is no need to find it separately.

Depending on the environment, $ may or may not be needed.

 batch.update( {team: lineUpPointsRoundRecord.teamId, round: 0}, { $setOnInsert: lineUpPointsGeneralRecord, $inc: {lfPoints: roundPoints}, $push: {roundPoints: roundPoints}, $upsert: true }); 
+1
source

All Articles