mongodbHow to bulk update documents in MongoDB?
MongoDB provides a bulk update operation to update multiple documents in a single operation. The syntax for bulk update is as follows:
db.collection.update(
<query>,
<update>,
{
upsert: <boolean>,
multi: <boolean>,
writeConcern: <document>
}
)
<query>
: Specifies the selection criteria for the update.<update>
: Specifies the modifications to be made to the documents that match the query.upsert
: Optional. If set to true, creates a new document when no document matches the query criteria.multi
: Optional. If set to true, updates multiple documents that meet the query criteria. If set to false, updates one document.writeConcern
: Optional. A document expressing the write concern.
For example, the following operation updates all documents in the collection that have a qty
field equal to 0 with an updatedQty
field equal to 20:
db.collection.update(
{ qty: 0 },
{ $set: { updatedQty: 20 } },
{ multi: true }
)
Helpful links
More of Mongodb
- How to check the version of MongoDB?
- How to use watch in MongoDB?
- How to update one document in MongoDB?
- How to update many documents in MongoDB?
- How to use MongoDB queue?
- How to create a many to many relation in MongoDB?
- How to insert new document into MongoDB?
- How to use hint in MongoDB?
- How to work with time series data in MongoDB?
- How to use unwind in MongoDB?
See more codes...