MongoDB – Update Document:
In MongoDB, updating a document can be done through below provided methods.
Syntax:
1 |
db.collection.updateOne() ---> New command in version 3.2. |
This command will update a single document even though the filter or condition have multiple document that may match to update.
1 |
db.collection.updateMany() ---> New command in version 3.2. |
This command will update all documents that match the condition.
1 |
db.collection.replaceOne() ---> New command in version 3.2. |
This command will replace a single document that match the condition.
1 |
db.collection.update() |
This command can update either a single document or multiple documents that match the condition.
Example:
1 2 3 4 5 6 7 |
db.students.update( { "firstname": "Manju" }, { $set: { "city": "delhi"}, $currentDate: { lastModified: true } } ) |