
- 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
Add a field to an embedded document in an array in MongoDB?
You can use update() along with $ operator for this. Let us first create a collection with documents −
> db.addAFieldDemo.insertOne( ... { ... ... "ClientName" : "Larry", ... "ClientCountryName" : "US", ... "ClientOtherDetails" : [ ... { ... "ClientProjectName":"Online Banking System" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd44bdc2cba06f46efe9ee8") }
Following is the query to display all documents from a collection with the help of find() method −
> db.addAFieldDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd44bdc2cba06f46efe9ee8"), "ClientName" : "Larry", "ClientCountryName" : "US", "ClientOtherDetails" : [ { "ClientProjectName" : "Online Banking System" } ] }
Following is the query to add a field to an embedded document in an array −
> db.addAFieldDemo.update({ClientOtherDetails:{$elemMatch:{"ClientProjectName" : "Online Banking System"}}}, ... {$set :{'ClientOtherDetails.$.isMarried':true}},true); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us display all the documents from the above collection −
> db.addAFieldDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd44bdc2cba06f46efe9ee8"), "ClientName" : "Larry", "ClientCountryName" : "US", "ClientOtherDetails" : [ { "ClientProjectName" : "Online Banking System", "isMarried" : true } ] }
- Related Articles
- How to add an extra field in a sub document in MongoDB?
- Increment a field in MongoDB document which is embedded?
- Implement MongoDB $push in embedded document array?
- How to add a field with specific datatype (list, object) in an existing MongoDB document?
- Add new field to every document in a MongoDB collection?
- Retrieving an embedded object as a document via the aggregation framework in MongoDB?
- MongoDB query to add a document in an already created collection
- How to add a sub-document to sub-document array in MongoDB?
- How to get embedded data in a MongoDB document?
- Filter query on array of embedded document with MongoDB?
- Return specific MongoDB embedded document
- MongoDB query for fields in embedded document?
- MongoDB query to push document into an array
- MongoDB query to add new array element in document
- MongoDB query to return only embedded document?

Advertisements