
- 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 exclude _id without including other fields using the aggregation framework in MongoDB?
Let us first create a collection with documents −
> db.excludeIdDemo.insertOne({"StudentFirstName":"John","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cd701a56d78f205348bc632") } > db.excludeIdDemo.insertOne({"StudentFirstName":"Robert","StudentAge":20}); { "acknowledged" : true, "insertedId" : ObjectId("5cd701af6d78f205348bc633") } > db.excludeIdDemo.insertOne({"StudentFirstName":"Chris","StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5cd701b86d78f205348bc634") }
Following is the query to display all documents from a collection with the help of find() method −
> db.excludeIdDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5cd701a56d78f205348bc632"), "StudentFirstName" : "John", "StudentAge" : 21 } { "_id" : ObjectId("5cd701af6d78f205348bc633"), "StudentFirstName" : "Robert", "StudentAge" : 20 } { "_id" : ObjectId("5cd701b86d78f205348bc634"), "StudentFirstName" : "Chris", "StudentAge" : 24 }
Following is the query to exclude _id without including other fields using the aggregation framework −
> db.excludeIdDemo.aggregate( { $project : { _id : 0, "StudentFirstName": 1 } } );
This will produce the following output −
{ "StudentFirstName" : "John" } { "StudentFirstName" : "Robert" } { "StudentFirstName" : "Chris" }
- Related Articles
- MongoDB query to group several fields using aggregation framework?
- Match between fields in MongoDB aggregation framework?
- Is it possible to sum two fields in MongoDB using the Aggregation framework?
- How to compare two fields in aggregation filter with MongoDB?
- Get the average of an entire field using the aggregation framework in MongoDB?
- MongoDB query to exclude both the fields with FALSE
- How to hide _id from Aggregation?
- MongoDB collection query to exclude some fields in find()?
- MongoDB query to display all the fields value, except _id
- Get Absolute value with MongoDB aggregation framework?
- MongoDB aggregation framework match OR is possible?
- MongoDB aggregation framework with group query example?
- MongoDB aggregation framework to sort by length of array?
- Is it possible to rename _id field after MongoDB group aggregation?
- Count by multiple fields with MongoDB aggregation

Advertisements