
- 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 can I change the field name in MongoDB?
To change the field name, use the $project. Let us create a collection with documents −
> db.demo517.insertOne({"Name":"Chris Brown"});{ "acknowledged" : true, "insertedId" : ObjectId("5e88a2a2987b6e0e9d18f595") } > db.demo517.insertOne({"Name":"David Miller"});{ "acknowledged" : true, "insertedId" : ObjectId("5e88a2ab987b6e0e9d18f596") } > db.demo517.insertOne({"Name":"John Doe"});{ "acknowledged" : true, "insertedId" : ObjectId("5e88a2b1987b6e0e9d18f597") }
Display all documents from a collection with the help of find() method −
> db.demo517.find();
This will produce the following output −
{ "_id" : ObjectId("5e88a2a2987b6e0e9d18f595"), "Name" : "Chris Brown" } { "_id" : ObjectId("5e88a2ab987b6e0e9d18f596"), "Name" : "David Miller" } { "_id" : ObjectId("5e88a2b1987b6e0e9d18f597"), "Name" : "John Doe" }
Following is the query to change the field name −
> db.demo517.aggregate([{$project:{FullName:"$Name"}}]);
This will produce the following output −
{ "_id" : ObjectId("5e88a2a2987b6e0e9d18f595"), "FullName" : "Chris Brown" } { "_id" : ObjectId("5e88a2ab987b6e0e9d18f596"), "FullName" : "David Miller" } { "_id" : ObjectId("5e88a2b1987b6e0e9d18f597"), "FullName" : "John Doe" }
- Related Articles
- How can I rename a field for all documents in MongoDB?
- How can we change a field name in JSON using Jackson in Java?
- How to change the type of a field in MongoDB?
- How can I check whether a field exists or not in MongoDB?
- How can I aggregate collection and group by field count in MongoDB?
- Change collection name in MongoDB?
- How can I drop a collection in MongoDB with two dashes in the name?
- How can I sort documents in MongoDB 4 and display only a single field?
- How can I change the name of an existing column from a MySQL table?
- MongoDB. max length of field name?
- How can I use MongoDB to find all documents which have a field, regardless of the value of that field?
- MongoDB - How can I see if all elements of a field are contained in a superset?
- How do I add a field to existing record in MongoDB?
- How can I change root username in MySQL?
- Can I utilize indexes when querying by MongoDB subdocument without known field names?

Advertisements