
- 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 add an extra field in a sub document in MongoDB?
You can use update(). Let us first create a collection with documents −
> db.addExtraFieldDemo.insertOne( { "_id": 1, "UserName": "Larry" , "UserOtherDetails":[ { "UserCountryName": "US", "UserAge":23 }, { "UserCountryName": "UK", "UserAge":24 } ] } ); { "acknowledged" : true, "insertedId" : 1 }
Following is the query to display all documents from a collection with the help of find() method −
> db.addExtraFieldDemo.find().pretty();
This will produce the following output −
{ "_id" : 1, "UserName" : "Larry", "UserOtherDetails" : [ { "UserCountryName" : "US", "UserAge" : 23 }, { "UserCountryName" : "UK", "UserAge" : 24 } ] }
Following is the query to add an extra field in a sub document in MongoDB −
> db.addExtraFieldDemo.update({ "_id":1},{ "$set":{ "UserOtherDetails.0.isMarried_" : true }}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the documents once again −
> db.addExtraFieldDemo.find().pretty();
This will produce the following output −
{ "_id" : 1, "UserName" : "Larry", "UserOtherDetails" : [ { "UserCountryName" : "US", "UserAge" : 23, "isMarried_" : true }, { "UserCountryName" : "UK", "UserAge" : 24 } ] }
- Related Articles
- How to add a sub-document to sub-document array in MongoDB?
- How to sum every field in a sub document of MongoDB?
- Add a field to an embedded document in an array in MongoDB?
- How to get distinct list of sub-document field values in MongoDB?
- Add new field to every document in a MongoDB collection?
- How to add a field with specific datatype (list, object) in an existing MongoDB document?
- Filter sub documents by sub document in MongoDB?
- MongoDB query to add a document in an already created collection
- Search a sub-field on MongoDB?
- How to remove a field completely from a MongoDB document?
- MongoDB query by sub-field?
- Sum MongoDB Sub-documents field?
- Increment a field in MongoDB document which is embedded?
- Find the MongoDB document from sub array?
- How to find a document by the non-existence of a field in MongoDB?

Advertisements