
- 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
Adding new property to each document in a large MongoDB collection?
You can use update command along with forEach() for large collection. Let us first create a collection with documents
>db.addingNewPropertyDemo.insertOne({"StudentName":"John","StudentAge":23,"CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1e61866324ffac2a7dc56") } >db.addingNewPropertyDemo.insertOne({"StudentName":"David","StudentAge":21,"CountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1e62366324ffac2a7dc57") } >db.addingNewPropertyDemo.insertOne({"StudentName":"Bob","StudentAge":21,"CountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1e62d66324ffac2a7dc58") }
Following is the query to display all documents from a collection with the help of find() method
> db.addingNewPropertyDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5ca1e61866324ffac2a7dc56"), "StudentName" : "John", "StudentAge" : 23, "CountryName" : "US" } { "_id" : ObjectId("5ca1e62366324ffac2a7dc57"), "StudentName" : "David", "StudentAge" : 21, "CountryName" : "AUS" } { "_id" : ObjectId("5ca1e62d66324ffac2a7dc58"), "StudentName" : "Bob", "StudentAge" : 21, "CountryName" : "UK" }
Following is the query to add new property to each document in a large collection
> db.addingNewPropertyDemo.find().forEach(function(data){ db.addingNewPropertyDemo.update({_id: data._id}, {$set: { StudentNameInUpperCase: data.StudentName.toUpperCase() }}) });
Let us check a new property is added or not
> db.addingNewPropertyDemo.find().pretty();
Following is the output displaying the new property as well
{ "_id" : ObjectId("5ca1e61866324ffac2a7dc56"), "StudentName" : "John", "StudentAge" : 23, "CountryName" : "US", "StudentNameInUpperCase" : "JOHN" } { "_id" : ObjectId("5ca1e62366324ffac2a7dc57"), "StudentName" : "David", "StudentAge" : 21, "CountryName" : "AUS", "StudentNameInUpperCase" : "DAVID" } { "_id" : ObjectId("5ca1e62d66324ffac2a7dc58"), "StudentName" : "Bob", "StudentAge" : 21, "CountryName" : "UK", "StudentNameInUpperCase" : "BOB" }
Look at the above sample output, StudentNameInUpperCase property is added.
- Related Articles
- Add new field to every document in a MongoDB collection?
- Updating a MongoDB document and adding new keys only in the first document?
- How to update a MongoDB document for adding a new item to an array?
- How to create a new collection in MongoDB?
- Retrieving the first document in a MongoDB collection?
- MongoDB query to update a specific document from a collection
- MongoDB query to add a document in an already created collection
- Accessing array values in a MongoDB collection to fetch a specific document
- How to insert a document into a MongoDB collection using Java?
- MongoDB query to add new array element in document
- How to delete document from a collection in MongoDB using deleteOne() method?
- Get the top most document from a MongoDB collection
- How to insert multiple document into a MongoDB collection using Java?
- MongoDB large collection and slow search? How to fix?
- How to update an existing document in MongoDB collection using Java?

Advertisements