Question : set with conditions in mongodb
Answered by : breakable-bison-1h7dk77elwrh
db.collectionname.aggregate( [ { $set: { //Compute the new value 'ResultField': { $cond: { if: { $lt: ['$ScoreField', 40] }, then: "Fail", else: "Pass" } } } }, { $out: 'collectionname' //Update the records in collection } ]
)
Source : https://stackoverflow.com/questions/30385663/mongodb-set-field-value-using-if-else-during-document-update | Last Update : Wed, 27 Oct 21