
- 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
MongoDB query to update nested document
Let us create a collection with documents −
> db.demo595.insertOne( { "Information": [ { "_id": new ObjectId(), Name:"Chris" }, { _id:new ObjectId(), Name:"Robert" } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5e93369cfd2d90c177b5bce4") }
Display all documents from a collection with the help of find() method −
> db.demo595.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e93369cfd2d90c177b5bce4"), "Information" : [ { "_id" : ObjectId("5e93369cfd2d90c177b5bce2"), "Name" : "Chris" }, { "_id" : ObjectId("5e93369cfd2d90c177b5bce3"), "Name" : "Robert" } ] }
Following is the query to update nested document −
>db.demo595.update({"Information._id":ObjectId("5e93369cfd2d90c177b5bce2")}, {$set:{"Info rmation.$.Name":"David Miller"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo595.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e93369cfd2d90c177b5bce4"), "Information" : [ { "_id" : ObjectId("5e93369cfd2d90c177b5bce2"), "Name" : "David Miller" }, { "_id" : ObjectId("5e93369cfd2d90c177b5bce3"), "Name" : "Robert" } ] }
- Related Articles
- MongoDB query to update the nested document?
- MongoDB find() query for nested document?
- MongoDB query to update a specific document from a collection
- In MongoDB how do you use $set to update a nested value/embedded document?
- Combine update and query parts to form the upserted document in MongoDB?
- Updating nested document in MongoDB
- Set condition in MongoDB nested document?
- MongoDB findOneAndUpdate() to update a single document
- MongoDB query to update tag
- MongoDB query to aggregate nested array
- MongoDB query to sort nested array?
- Perform nested document value search in MongoDB?
- MongoDB query to update selected fields
- MongoDB query to return only embedded document?
- MongoDB query to remove a specific document

Advertisements