Now there is a requirement to use MongoDB to find documents and update the fields in the documents at the same time, that is, to use a command to complete the search and update (atomicity).
In Mongo, there are similar methods for a single document, such as FindAndModify search and update, which are atomic but only apply to a single document.
Ask if it is possible to find multiple documents and update the fields of the documents accordingly.
I. Atomicity in MongoDB:
1. For a single document, it is atomic. FindAnyModify operates only one document, so it is atomic.
2. For operations on multiple documents, each operation on a single document is atomic, but the whole operation is not atomic, and there may be other operations on documents in the operation process.
However, in most scenarios, the atomicity of a single document has already met the requirements.
If you need to atomize the operation of multiple documents, you need to cooperate with your own code/design.
Second, according to your expressed needs, you can actually use the multi option of update or directly use updateMany.
db.collection.updateMany() db.collection.update(,{multi : true})
For reference.
Love MongoDB! Have Fun!