

- 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
How do I return a document with filtered sub-documents using Mongo?
<p>For this, use $project in MongoDB. Within that, use $filter. Let us create a collection with documents −</p><pre class="prettyprint notranslate">> 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 }</pre><p>Display all documents from a collection with the help of find() method −</p><pre class="prettyprint notranslate">> db.demo457.find();</pre><p>This will produce the following output −</p><pre class="result notranslate">{ "_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 } ] }</pre><p>Following is the query to return a document with filtered sub-documents using MongoDB −</p><pre class="prettyprint notranslate" style="">> db.demo457.aggregate([ ... { ... $project: { ... details: { ... $filter: { ... input: "$details", ... as: "output", ... cond: { $gte: [ "$$output.ProductPrice", 170 ] } ... } ... } ... } ... } ... ])</pre><p>This will produce the following output −</p><pre class="result notranslate">{ "_id" : 101, "details" : [ { "ProductName" : "Product-2", "ProductPrice" : 190 } ] } { "_id" : 102, "details" : [ { "ProductName" : "Product-4", "ProductPrice" : 360 } ] }</pre>
- Related Questions & Answers
- Filter sub documents by sub document in MongoDB?
- How would I filter out sub documents in MongoDB?
- How to add a sub-document to sub-document array 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 return multiple results in a MySQL subquery with IN()?
- 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 delete array value from a document in MongoDB?
- Working with MongoDB $sort for sub array document
- Sum MongoDB Sub-documents field?
- MongoDB filter multiple sub-documents?
- How do I remove a string from an array in a MongoDB document?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
Advertisements