dimanche 5 avril 2015

mongodb select either both documents or none

I am in situation where I have to update either two documents or none of them, how is it possible to implement such behavior with mongo?



// nodejs mongodb driver
Bus.update({
"_id": { $in: [ObjectId("abc"), ObjectId("def")] },
"seats": { $gt: 0 }
}, {
$inc: { "seats": -1 }
}, { multi: true }, function(error, update) {
assert(update.result.nModified === 2)
})


The problem with code above it will update even if only one bus matched. In my case I try to book ticket for bus in both directions and should fail if at least one of them already fully booked.


Thank you


Aucun commentaire:

Enregistrer un commentaire