Now there is a need to use MongoDB to search for documents and update the fields in the document at the same time, that is, to use one command to complete the search and update (atomicity).
Mongo has similar methods for a single document, such as the FindAndModify search and update operation, which is atomic, but only affects a single document.
I would like to ask if there is any operation that can find multiple documents and update the fields of the documents accordingly.
Mongoose update can be operated in batches. The multi attribute of update is set to true, http://www.nonb.cn/blog/nodej...
1. Atomicity in MongoDB:
1. It is atomic for a single document. FindAnyModify only operates one document, so it is atomic;
2. For operations on multiple documents, each operation on a single document is atomic, but the entire operation is not atomic. During the operation, there may be operations from other documents.
But in most scenarios, the atomicity of a single document already meets the requirements;
If you need to achieve atomicity for the operation of multiple documents, you need to implement it with your own code/design.
2. For your expressed needs, you can actually use the multi option of update, or directly use updateMany.
For reference.
Love MongoDB! Have Fun!