How to properly do a Bulk upsert/update in MongoDB

前端 未结 3 625
梦毁少年i
梦毁少年i 2020-12-10 14:53

I\'m trying to:

  • Find a document according to a search criteria,
  • If found, update some attributes
  • If not insert a document with some attribut
3条回答
  •  庸人自扰
    2020-12-10 15:26

    Typically I have always set upsert as a property on update. Also update should be able to find the record itself so no need to find it individually.

    Depending on the environment the $ may or may not be necessary.

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

提交回复
热议问题