MongoDB update with condition

前端 未结 6 684
鱼传尺愫
鱼传尺愫 2020-11-29 10:11

I\'m trying to update some field in my collection depending on a condition.

I want to set field active to true if the condition is tr

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 10:34

    Of course you can.... by running 2 queries

    db.collection.update({condition}, { $set: { state } }, { multi: true });
    db.collection.update({!condition}, { $set: { state } }, { multi: false });
    

    for your example case

    db.consent.update(
      {"_id": ObjectId("5714ce0a4514ef3ef68677fd")},
      { $set: { "active": true } });
    
    db.consent.update(
      {"_id": {$ne: ObjectId("5714ce0a4514ef3ef68677fd")}},
      { $set: { "active": false } },
      { multi: true });
    

提交回复
热议问题