
- 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 find specific array elements in MongoDB document with query and filter with range?
For this, use aggregate() in MongoDB. Let us create a collection with documents −
> db.demo351.insertOne( ... { ... ... "_id" : "101", ... "ProductDetails" : [ ... { ... "ProductName" : "Product-1", ... "ProductPrice" :500 ... }, ... { ... "ProductName" : "Product-2", ... "ProductPrice" :400 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : "101" } > db.demo351.insertOne( ... { ... ... "_id" : "102", ... "ProductDetails" : [ ... { ... "ProductName" : "Product-3", ... "ProductPrice" :200 ... }, ... { ... "ProductName" : "Product-4", ... "ProductPrice" :800 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : "102" }
Display all documents from a collection with the help of find() method &mnus;
> db.demo351.find();
This will produce the following output −
{ "_id" : "101", "ProductDetails" : [ { "ProductName" : "Product-1", "ProductPrice" : 500 }, { "ProductName" : "Product-2", "ProductPrice" : 400 } ] } { "_id" : "102", "ProductDetails" : [ { "ProductName" : "Product-3", "ProductPrice" : 200 }, { "ProductName" : "Product-4", "ProductPrice" : 800 } ] }
Following is the query to find specific array elements in MongoDB document with query and filter with range −
> db.demo351.aggregate([ ... { ... $match: { _id: "102" } ... }, ... { ... $addFields: { ... ProductDetails: { ... $filter: { ... input: "$ProductDetails", ... cond: { ... $and: [ ... { $gt: [ "$$this.ProductPrice", 600 ] }, ... { $lt: [ "$$this.ProductPrice", 900 ] } ... ] ... } ... } ... } ... } ... } ... ])
This will produce the following output −
{ "_id" : "102", "ProductDetails" : [ { "ProductName" : "Product-4", "ProductPrice" : 800 } ] }
- Related Articles
- Filter query on array of embedded document with MongoDB?
- Fetch a specific document in MongoDB with array elements
- How to filter a query on specific date format with MongoDB?
- MongoDB query to find value in array with multiple criteria (range)
- Find document with array that contains a specific value in MongoDB
- MongoDB query to filter by several array elements?
- MongoDB query to remove array elements from a document?
- Filter specific values from a MongoDB document
- Update a specific MongoDB document in array with $set and positional $ operator?
- How to project specific elements in an array field with MongoDB?
- MongoDB query to find documents with specific FirstName and LastName
- How to filter array in subdocument with MongoDB?
- How to filter array elements in MongoDB?
- Python – Filter Rows with Range Elements
- MongoDB query to remove a specific document

Advertisements