
- 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
Is it possible to rename _id field after MongoDB group aggregation?
Yes, it is possible to rename using aggregation. Let us first create a collection with documents
> db.renameIdDemo.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a1760353decbc2fc927c5") } > db.renameIdDemo.insertOne({"StudentName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a1765353decbc2fc927c6") } > db.renameIdDemo.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a176b353decbc2fc927c7") }
Following is the query to display all documents from a collection with the help of find() method
> db.renameIdDemo.find();
This will produce the following output
{ "_id" : ObjectId("5c9a1760353decbc2fc927c5"), "StudentName" : "Chris" } { "_id" : ObjectId("5c9a1765353decbc2fc927c6"), "StudentName" : "Robert" } { "_id" : ObjectId("5c9a176b353decbc2fc927c7"), "StudentName" : "David" }
Following is the query to rename _id field:
> db.renameIdDemo.aggregate({ $project: { ... _id: 0, ... mainId: "$_id", ... count: 1, ... sum: 1 ... } ... } ... );
This will produce the following output. We have renamed _id to mainId;
{ "mainId" : ObjectId("5c9a1760353decbc2fc927c5") } { "mainId" : ObjectId("5c9a1765353decbc2fc927c6") } { "mainId" : ObjectId("5c9a176b353decbc2fc927c7") }
- Related Articles
- Is it possible to sum two fields in MongoDB using the Aggregation framework?
- MongoDB aggregation framework match OR is possible?
- Is it possible to use MongoDB field value as pattern in $regex?
- Is it possible to use MongoDB field value as a pattern in $regex?
- Hide id field in MongoDB
- MongoDB aggregation framework with group query example?
- MongoDB query to group several fields using aggregation framework?
- MongoDB aggregation to fetch documents with specific field value?
- Is it possible to delete everything after a 'space' in a MySQL field?
- Sort and Group in one MongoDB aggregation query?
- MongoDB aggregation group and remove duplicate array values?
- MongoDB query (aggregation framework) to match a specific field value
- Is it possible to use MongoDB capped collection?
- Is it possible to cast in a MongoDB Query?
- MongoDB query to exclude if id is equal to a document field array value

Advertisements