In a nested MongoDB call, how can I ensure atomicity?

Is it possible to automatically update / delete two documents in MongoDB, causing a new update / delete call from the first update callback? In the example below, I want to remove the second document from the collection, but only if the update of the first document is successfully completed:

db.collection.update(conditions1, {$set: set}, function (err,result){ db.collection.remove(conditions2, function(err,doc_num){ db.close(); )}; }); 

I come across the operator of $ isolated requests, but from what I understand in the documentation, this operator is used to perform read / write locks on one request that affects several documents, and not when reading / writing, block one document after updating on another document using the first document update callback that I want to try and execute.

+5
source share
2 answers

No, that’s not possible, because. As described here, the lock will be implemented on one request, and not on the whole transaction.

You can overcome the atomicity problem by using this .

0
source

As Amir said, this is not possible, but you can imitate the behavior in mangoes following a two-stage fixation scheme . This link also refers to how to perform rollback operations.

0
source

Source: https://habr.com/ru/post/1238004/


All Articles