
- 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 do I add a field to existing record in MongoDB?
You can use update command to add a field to existing record. Let us first create a collection with documents −
> db.addAFieldToEveryRecordDemo.insertOne({"ClientName":"Chris","ClientAge":34}); { "acknowledged" : true, "insertedId" : ObjectId("5cd00e32588d4a6447b2e061") } > db.addAFieldToEveryRecordDemo.insertOne({"ClientName":"Robert","ClientAge":36}); { "acknowledged" : true, "insertedId" : ObjectId("5cd00e59588d4a6447b2e062") }
Following is the query to display all documents from a collection with the help of find() method −
> db.addAFieldToEveryRecordDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd00e32588d4a6447b2e061"), "ClientName" : "Chris", "ClientAge" : 34 } { "_id" : ObjectId("5cd00e59588d4a6447b2e062"), "ClientName" : "Robert", "ClientAge" : 36 }
Here is the query to add a field to every record. We are adding ClientDetails −
>db.addAFieldToEveryRecordDemo.update({},{$set:{"ClientDetails.ClientCountryName":""}},true,true); WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
Let us check all documents have been added with new record or not −
> db.addAFieldToEveryRecordDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd00e32588d4a6447b2e061"), "ClientName" : "Chris", "ClientAge" : 34, "ClientDetails" : { "ClientCountryName" : "" } } { "_id" : ObjectId("5cd00e59588d4a6447b2e062"), "ClientName" : "Robert", "ClientAge" : 36, "ClientDetails" : { "ClientCountryName" : "" } }
- Related Articles
- How to add a field with specific datatype (list, object) in an existing MongoDB document?
- How do I push elements to an existing array in MongoDB?
- How to update record in MongoDB without replacing the existing fields?
- Remove and update the existing record in MongoDB?
- How do I check whether a field contains null value in MongoDB?
- How to update field to add value to existing value in MySQL?
- How to search for a record (field) and then delete it in MongoDB?
- How do I add a value to the top of an array in MongoDB?
- How do I order a list and add position to its items in MongoDB?
- How do I exclude a specific record in MySQL?
- How do I find all documents with a field that is NaN in MongoDB?
- How to add an extra field in a sub document in MongoDB?
- How can I add a Boolean field to MySQL?
- How to add a random number between 30 and 300 to an existing field in MySQL?
- How to add a new field to all the documents in a MongoDB collection

Advertisements