
- 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 re-map the fields of a MongoDB collection?
To re-map the fields of a MongoDB collection, use update() along with $rename. Let us first create a collection with documents −
> db.demo171.insertOne( { "Name": "Chris", "Details": { "SubjectName": "MySQL", "CountryName": "US" } } ); { "acknowledged" : true, "insertedId" : ObjectId("5e3837 399e4f06af551997e0") }
Display all documents from a collection with the help of find() method −
> db.demo171.find();
This will produce the following output −
{ "_id" : ObjectId("5e3837399e4f06af551997e0"), "Name" : "Chris", "Details" : { "SubjectName" : "MySQL", "CountryName" : "US" } }
Following is the query to re-map the fields of a MongoDB collection −
> db.demo171.update({}, { $rename : { 'Name' : 'StudentName'} }, false, true); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo171.find();
This will produce the following output −
{ "_id" : ObjectId("5e3837399e4f06af551997e0"), "Details" : { "SubjectName" : "MySQL", "CountryName" : "US" }, "StudentName" : "Chris" }
- Related Articles
- How to retrieve all nested fields from MongoDB collection?
- Get all fields names in a MongoDB collection?
- MongoDB collection query to exclude some fields in find()?
- How to find MongoDB documents in a collection with a filter on multiple combined fields?
- How to drop a collection in MongoDB?
- How to count the number of documents in a MongoDB collection?
- How to sort the documents of a MongoDB collection using java?
- How to find datatype of all the fields in MongoDB?
- How to get the child of a MongoDB collection by the key?
- How to drop a numeric collection from MongoDB?
- How to create a new collection in MongoDB?
- How to get array from a MongoDB collection?
- How to add a column in MongoDB collection?
- MongoDB: How to query a collection named “version”?
- How to create a MongoDB collection using Java?

Advertisements