
- 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 new field to every document in a MongoDB collection?
To add new field to every document in a MongoDB collection, you can use $set operator. The syntax is as follows:
db.yourCollectionName.update({}, { $set: {"yourFieldName": yourValue} }, false, true);
To understand the above syntax, let us create a collection with some documents. The query to create a collection with documents is as follows:
>db.addNewFieldToEveryDocument.insertOne({"StudentName":"John","StudentAddress":"US "}); { "acknowledged" : true, "insertedId" : ObjectId("5c6efc0b6fd07954a48906ae") } >db.addNewFieldToEveryDocument.insertOne({"StudentName":"David","StudentAddress":"U K"}); { "acknowledged" : true, "insertedId" : ObjectId("5c6efc0b6fd07954a48906af") } >db.addNewFieldToEveryDocument.insertOne({"StudentName":"Carol","StudentAddress":"U K"}); { "acknowledged" : true, "insertedId" : ObjectId("5c6efc0b6fd07954a48906b0") } >db.addNewFieldToEveryDocument.insertOne({"StudentName":"Bob","StudentAddress":"US" }); { "acknowledged" : true, "insertedId" : ObjectId("5c6efc0b6fd07954a48906b1") }
Display all documents from a collection with the help of find() method. The query is as follows:
> db.addNewFieldToEveryDocument.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6efc0b6fd07954a48906ae"), "StudentName" : "John", "StudentAddress" : "US" } { "_id" : ObjectId("5c6efc0b6fd07954a48906af"), "StudentName" : "David", "StudentAddress" : "UK" } { "_id" : ObjectId("5c6efc0b6fd07954a48906b0"), "StudentName" : "Carol", "StudentAddress" : "UK" } { "_id" : ObjectId("5c6efc0b6fd07954a48906b1"), "StudentName" : "Bob", "StudentAddress" : "US" }
The following is the query to add a new field to every document:
> db.addNewFieldToEveryDocument.update({}, { $set: {"StudentAge": 24} }, false, true); WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 })
Above, we have added a new field “StudentAge”:24 in every document. Let us check the field “StudentAge”:24 is successfully added to every document or not. The query is as follows:
> db.addNewFieldToEveryDocument.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6efc0b6fd07954a48906ae"), "StudentName" : "John", "StudentAddress" : "US", "StudentAge" : 24 } { "_id" : ObjectId("5c6efc0b6fd07954a48906af"), "StudentName" : "David", "StudentAddress" : "UK", "StudentAge" : 24 } { "_id" : ObjectId("5c6efc0b6fd07954a48906b0"), "StudentName" : "Carol", "StudentAddress" : "UK", "StudentAge" : 24 } { "_id" : ObjectId("5c6efc0b6fd07954a48906b1"), "StudentName" : "Bob", "StudentAddress" : "US", "StudentAge" : 24 }
- Related Articles
- How to add a new field to all the documents in a MongoDB collection
- Adding new property to each document in a large MongoDB collection?
- How to sum every field in a sub document of MongoDB?
- MongoDB query to add a document in an already created collection
- MongoDB query to add new array element in document
- How to add an extra field in a sub document in MongoDB?
- Add a field to an embedded document in an array in MongoDB?
- Add field that is unique index to collection in MongoDB?
- Include all existing fields and add new fields to document in MongoDB?
- How to create a new collection in MongoDB?
- Retrieving the first document in a MongoDB collection?
- How to add a field with specific datatype (list, object) in an existing MongoDB document?
- How to add a sub-document to sub-document array in MongoDB?
- How to check empty field in a MongoDB collection?
- How to add a column in MongoDB collection?

Advertisements