Updating Documents
Estimated time to read: 3 minutes
Sub-Documents
Array Fields An array of objects within an array field is a common way of storing data for consumption within an application.
Data Explorer¶
Within the Atlas Data Explorer, a query can be performed on a collection to return either a single or a number of documents. A document can then be modified within the UI using the edit tool.
The UI walks though the usage of the Data Explorer and presents options to add fields, edit fields or update other elements.
The document can be reviewed before being updated via the UI, the update can then be applied using the Update
button in the UI.
Mongo Shell¶
Documents can also be updated using the Mongo Shell through the usage of MQL (Mongo Query Language).
There are two commands for updating documents within MongoDB, these are:
updateOne()
- To update an individual documents within a collection. If multiple documents are valid targets, the first document found by the cursor will have the update applied, any following targets will not be updated. updateMany()
- To update multiple documents within a collection. All documents that match the given query will be updated.
Update Operators¶
MQL has a number of operators that can be used to manipulate documents in the collection. For example:
$inc
can be used to increment values within a document
$set
can be used to add or update the given fields value with a exact value.
$push
can be used to add an element to an array field
Population Update Example¶
mongo "mongodb+srv://<username>:<password>@<cluster>.mongodb.net/admin"
- Connect to your Atlas Cluster.
use sample_training
- Use the sample_training database as your database in the following commands.
db.zips.find({ "zip": "12534" }).pretty()
- Find all documents in the zips collection where the zip field is equal to "12434".
db.zips.find({ "city": "HUDSON" }).pretty()
- Find all documents in the zips collection where the city field is equal to "HUDSON".
db.zips.find({ "city": "HUDSON" }).count()
- Find how many documents in the zips collection have the city field equal to "HUDSON".
db.zips.updateMany({ "city": "HUDSON" }, { "$inc": { "pop": 10 } })
- Update all documents in the zips collection where the city field is equal to "HUDSON" by adding 10 to the current value of the "pop" field.
db.zips.updateOne({ "zip": "12534" }, { "$set": { "pop": 17630 } })
- Update a single document in the zips collection where the zip field is equal to "12534" by setting the value of the "pop" field to 17630.
db.zips.updateOne({ "zip": "12534" }, { "$set": { "population": 17630 } })
- Update a single document in the zips collection where the zip field is equal to "12534" by setting the value of the "population" field to 17630.
Grade Update Example¶
db.grades.find({ "student_id": 151, "class_id": 339 }).pretty()
- Find all documents in the grades collection where the student_id field is 151 , and the class_id field is 339.
db.grades.find({ "student_id": 250, "class_id": 339 }).pretty()
- Find all documents in the grades collection where the student_id field is 250 , and the class_id field is 339.
JSON db.grades.updateOne({ "student_id": 250, "class_id": 339 }, { "$push": { "scores": { "type": "extra credit", "score": 100 } } })
- Update one document in the grades collection where the student_id is 250
*, and the class_id field is 339 , by adding a document element to the "scores" array.