I would like to ask if there is a function in MongoDB to search documents in batches and then update the values ​​of specified fields in the documents?
为情所困
为情所困 2017-05-02 09:25:48
0
2
660

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.

为情所困
为情所困

reply all(2)
phpcn_u1582

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.

db.collection.updateMany()

db.collection.update(,{multi : true})

For reference.

Love MongoDB! Have Fun!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template