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 list MongoDB users?
- How to remove a field from MongoDB?
- How to create a many to many relation in MongoDB?
- How to use MongoDB queue?
- What is MongoDB default port?
- How to update an array element in MongoDB?
- How to select specific fields in MongoDB query?
- How to check if array is empty in MongoDB?
See more codes...