Update Documents: Part 1
Understand how to update documents in MongoDB using updateOne and updateMany methods. Learn to apply filter queries, modify fields, use options like upsert, and interpret operation results for effective data management.
We'll cover the following...
We'll cover the following...
Update single document
We use the below commands to update a single document.
db.<collection-name>.updateOne(
<filter query>,
<update document>,
<options>
);
Filter query
filter query is used to build criteria to fetch documents. This should return only one document if used with the updateOne method.
Update document
The updated document contains modifications that need to apply to the document. Through this document, we can:
- Replace a field value
- Add a new field
- Increment or decrement a number
- Add a new value in the array
- Remove a value from the array
- Perform modification on