
- 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 do I return a document with filtered sub-documents using Mongo?
For this, use $project in MongoDB. Within that, use $filter. Let us create a collection with documents −
> db.demo457.insertOne( ... { ... _id: 101, ... details: [ ... { ProductName:"Product-1" , ProductPrice:90 }, ... { ProductName:"Product-2" , ProductPrice:190 } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 101 } > > db.demo457.insertOne( ... { ... _id: 102, ... details: [ ... { ProductName:"Product-3" , ProductPrice:150}, ... { ProductName:"Product-4" , ProductPrice:360 } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 102 }
Display all documents from a collection with the help of find() method −
> db.demo457.find();
This will produce the following output −
{ "_id" : 101, "details" : [ { "ProductName" : "Product-1", "ProductPrice" : 90 }, { "ProductName" : "Product-2", "ProductPrice" : 190 } ] } { "_id" : 102, "details" : [ { "ProductName" : "Product-3", "ProductPrice" : 150 }, { "ProductName" : "Product-4", "ProductPrice" : 360 } ] }
Following is the query to return a document with filtered sub-documents using MongoDB −
> db.demo457.aggregate([ ... { ... $project: { ... details: { ... $filter: { ... input: "$details", ... as: "output", ... cond: { $gte: [ "$$output.ProductPrice", 170 ] } ... } ... } ... } ... } ... ])
This will produce the following output −
{ "_id" : 101, "details" : [ { "ProductName" : "Product-2", "ProductPrice" : 190 } ] } { "_id" : 102, "details" : [ { "ProductName" : "Product-4", "ProductPrice" : 360 } ] }
- Related Articles
- Filter sub documents by sub document in MongoDB?
- How to add a sub-document to sub-document array in MongoDB?
- How would I filter out sub documents in MongoDB?
- How do I insert a record from one Mongo database into another?
- How to merge array of documents in Mongo DB?
- How do I find all documents with a field that is NaN in MongoDB?
- How do I get email-id from a MongoDB document and display with print()
- How do I loop through a JSON file with multiple keys/sub-keys in Python?
- How do I return multiple results in a MySQL subquery with IN()?
- Working with MongoDB $sort for sub array document
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- How do I delete array value from a document in MongoDB?
- MongoDB filter multiple sub-documents?
- Sum MongoDB Sub-documents field?
- How do I remove a string from an array in a MongoDB document?

Advertisements