
- 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 exclude nested fields in MongoDB with a wildcard?
Achieve this with aggregation pipeline. Let us first create a collection with documents −
> db.demo413.insertOne( ... { ... "_id": "101", ... "details": { ... "Info1": { ... Name:"Chris", ... Age:21 ... }, ... "Info2": { ... Name:"David", ... Age:23 ... } ... } ... } ... ); { "acknowledged" : true, "insertedId" : "101" }
Display all documents from a collection with the help of find() method −
> db.demo413.find();
This will produce the following output −
{ "_id" : "101", "details" : { "Info1" : { "Name" : "Chris", "Age" : 21 }, "Info2" : { "Name" : "David", "Age" : 23 } } }
Following is the query to exclude nested fields −
> db.demo413.aggregate([ ... { $project: { "details" : { $objectToArray: "$details" } } }, ... { $project: { "details.v.Age" : 0} }, ... { $project: { "details" : { $arrayToObject: "$details"} } } ... ]);
This will produce the following output −
{ "_id" : "101", "details" : { "Info1" : { "Name" : "Chris" }, "Info2" : { "Name" : "David" } } }
- Related Articles
- MongoDB query to exclude both the fields with FALSE
- MongoDB collection query to exclude some fields in find()?
- Is it possible to sum two fields in MongoDB using the Aggregation framework?
- How to group nested fields in MongoDB aggregation with count value in array?
- Is it possible to cast in a MongoDB Query?
- How to retrieve all nested fields from MongoDB collection?
- MongoDB query to get only specific fields in nested array documents?
- Is it possible to exclude subclasses from the results displayed in backoffice in SAP?
- Is it possible to achieve a slice chain in MongoDB?
- How to exclude _id without including other fields using the aggregation framework in MongoDB?
- Is it possible to make a case-insensitive query in MongoDB?
- Is it possible to use MongoDB capped collection?
- Is it possible to write to MongoDB console in JavaScript execution?
- Is it possible to use MongoDB field value as a pattern in $regex?
- Pull and add to set at the same time with MongoDB? Is it Possible?

Advertisements