

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- MongoDB query to exclude both the fields with FALSE
- Is it possible to sum two fields in MongoDB using the Aggregation framework?
- MongoDB collection query to exclude some fields in find()?
- Is it possible to cast in a MongoDB Query?
- Is it possible to achieve a slice chain in MongoDB?
- Is it possible to use MongoDB capped collection?
- Is it possible to make a nested object immutable using Object.freeze() in JavaScript?
- Is it possible to exclude subclasses from the results displayed in backoffice in SAP?
- Is it possible to make a case-insensitive query in MongoDB?
- How to group nested fields in MongoDB aggregation with count value in array?
- How to retrieve all nested fields from MongoDB collection?
- Is it possible to write to MongoDB console in JavaScript execution?
- MongoDB query to get only specific fields in nested array documents?
- Is it possible to use MongoDB field value as a pattern in $regex?
- How to exclude _id without including other fields using the aggregation framework in MongoDB?
Advertisements