I have to update two collections. I want to make sure that either both the collections get updated or they don’t.
I created this simple example of two updates (both of them use same collection here, but they could be different):
await this.client.connect();
const session = this.client.startSession();
try{
await session.withTransaction(async () => {
await this.client.db("Person").collection("persons").updateMany({ "phone": "23138213"}, {$set: {"gender": "Male"} }, function(err, res) {
if (err) throw err;
console.log(res.result.nModified + " document(s) updated");
});
await this.client.db("Person").collection("persons").updateMany({ "phone": "23138213"}, "Other" , function(err, res) {
if (err) throw err;
console.log(res.result.nModified + " document(s) updated");
});
})
}
finally{
await session.endSession();
}
Now if the initial value of gender was empty ""
. Then after executing the above code, the final value should still be ""
because the second update is invalid syntax and would throw exception.
But the result is gender:Male