MongoDB: db.collection.update() method
db.collection.update
The db.collection.update() method is used to modify an existing document or documents in a collection. The method can modify specific fields of an existing document or documents or replace an existing document entirely, depending on the update parameter. By default, the update() method updates a single document. Set the Multi-Parameter to update all documents that match the query criteria.
Syntax:
db.collection.update(query, update, options)
Parameters:
Name | Description | Required / Optional |
Type |
---|---|---|---|
query | The selection criteria for the update. The same query selectors as in the find() method are available. | Required | document |
update | The modifications to apply. | Required | document |
upsert | If set to true, creates a new document when no document matches the query criteria. The default value is false, which does not insert a new document when no match is found. | Optional | boolean |
multi | If set to true, updates multiple documents that meet the query criteria. If set to false, updates one document. The default value is false. | Optional | boolean |
writeConcern | A document expressing the write concern. Omit to use the default write concern. | Optional | document |
Example: MongoDB: db.collection.update() method
Sample document in the restaurants collection:
{
"address": {
"building": "1007",
"coord": [ -73.856077, 40.848447 ],
"street": "Morris Park Ave",
"zipcode": "10462"
},
"borough": "Bronx",
"cuisine": "Bakery",
"grades": [
{ "date": { "$date": 1393804800000 }, "grade": "A", "score": 2 },
{ "date": { "$date": 1378857600000 }, "grade": "A", "score": 6 },
{ "date": { "$date": 1358985600000 }, "grade": "A", "score": 10 },
{ "date": { "$date": 1322006400000 }, "grade": "A", "score": 9 },
{ "date": { "$date": 1299715200000 }, "grade": "B", "score": 14 }
],
"name": "Morris Park Bake Shop",
"restaurant_id": "30075445"
}
Retrieve the restaurants data from here
Behavior
Safe Writes
The update() method uses the update command, which uses the default write concern. To specify a different write concern, include the writeConcern option in the options parameter.
Update Parameter
The update() method either modifies specific fields in existing documents or replaces an existing document entirely.
Update Specific Fields
If the <update> document contains update operator modifiers, such as those using the $set modifier, then:
- The <update> document must contain only update operator expressions.
- The update() method updates only the corresponding fields in the document.
To update an embedded document or an array as a whole, specify the replacement value for the field. To update particular fields in an embedded document or in an array, use dot notation to specify the field.
Previous:
db.collection. totalIndexSize() method
Next:
db.collection.validate() method
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join