
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How to update value of a key in a list of a json in MongoDB?
Let us first create a collection with documents
> db.updateListOfKeyValuesDemo.insertOne( { "StudentDetails":[ { "StudentName":"John", "StudentAge":23, "StudentCountryName":"US" }, { "StudentName":"Carol", "StudentAge":24, "StudentCountryName":"UK" }, { "StudentName":"Bob", "StudentAge":22, "StudentCountryName":"AUS" } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5c9b5b759882024390176545") }
Following is the query to display all documents from a collection with the help of find() method
> db.updateListOfKeyValuesDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9b5b759882024390176545"), "StudentDetails" : [ { "StudentName" : "John", "StudentAge" : 23, "StudentCountryName" : "US" }, { "StudentName" : "Carol", "StudentAge" : 24, "StudentCountryName" : "UK" }, { "StudentName" : "Bob", "StudentAge" : 22, "StudentCountryName" : "AUS" } ] }
Following is the query to update value of a key in a list of a json in MongoDB
> var documentFromCollection = db.updateListOfKeyValuesDemo.findOne({ ... "_id": ObjectId("5c9b5b759882024390176545") ... }); > > documentFromCollection.StudentDetails.forEach(function(updateStudent) { ... updateStudent.StudentName = "Ramit"; ... }); > db.updateListOfKeyValuesDemo.update( ... { "_id": documentFromCollection ._id }, ... { "$set": { "StudentDetails": documentFromCollection.StudentDetails } } ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the list of key values has been updated or not
> db.updateListOfKeyValuesDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9b5b759882024390176545"), "StudentDetails" : [ { "StudentName" : "Ramit", "StudentAge" : 23, "StudentCountryName" : "US" }, { "StudentName" : "Ramit", "StudentAge" : 24, "StudentCountryName" : "UK" }, { "StudentName" : "Ramit", "StudentAge" : 22, "StudentCountryName" : "AUS" } ] }
- Related Articles
- How to update the value of a key in a dictionary in Python?
- Updating a set of documents from a list of key value pairs in MongoDB
- Update key value where another key equals some value in MongoDB?
- Update key value where different key equals some value in MongoDB?
- Update a single list item of a MongoDB document?
- Update object in array with a specific key in MongoDB
- How to sum the value of a key across all documents in a MongoDB collection?
- Update only a specific value in a MongoDB document
- How to read a specific key-value pair from a MongoDB collection?
- How to update the _id of a MongoDB Document?
- How to access subdocument value when the key is a number in MongoDB?
- How to run MongoDB query to update only a specific field value?
- In MongoDB how do you use $set to update a nested value/embedded document?
- How to retrieve a value from MongoDB by its key name?
- How we can update a Python list element value?

Advertisements