
- 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
Querying array of Embedded Documents in MongoDB based on Range?
To query an array of embedded documents based on range, use aggregate(). Let us create a collection with documents −
> db.demo346.insertOne( ... { ... _id: 101, ... userDetails: [ ... { UserName: "Chris", Score:78}, ... { UserName: "David", Score:68}, ... { UserName: "Bob", Score:88} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 101 } > db.demo346.insertOne( ... { ... _id: 102, ... userDetails: [ ... { UserName: "Mike", Score:92}, ... { UserName: "Sam", Score:62}, ... { UserName: "Carol", Score:97} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 102 }
Display all documents from a collection with the help of find() method −
> db.demo346.find();
This will produce the following output −
{ "_id" : 101, "userDetails" : [ { "UserName" : "Chris", "Score" : 78 }, { "UserName" : "David", "Score" : 68 }, { "UserName" : "Bob", "Score" : 88 } ] } { "_id" : 102, "userDetails" : [ { "UserName" : "Mike", "Score" : 92 }, { "UserName" : "Sam", "Score" : 62 }, { "UserName" : "Carol", "Score" : 97 } ] }
Following is how to query an array of embedded documents in MongoDB based on Range −
> db.demo346.aggregate([ ... { "$match": { "$expr": { "$gte": [{ "$size": { "$ifNull": ["$userDetails", []] } }, 1] }}}, ... { "$addFields": { ... "userDetails": { ... "$filter": { ... "input": { "$ifNull": ["$userDetails", []] }, ... "cond": { ... "$and": [ ... { "$gte": ["$$this.Score", 80] }, ... { "$lte": ["$$this.Score", 99] } ... ] ... } ... } ... } ... }} ... ])
This will produce the following output −
{ "_id" : 101, "userDetails" : [ { "UserName" : "Bob", "Score" : 88 } ] } { "_id" : 102, "userDetails" : [ { "UserName" : "Mike", "Score" : 92 }, { "UserName" : "Carol", "Score" : 97 } ] }
- Related Articles
- Querying on an array of objects for specific nested documents with MongoDB?
- MongoDB - Query embedded documents?
- How to filter documents based on an array in MongoDB?
- Query an array of embedded documents in MongoDB and push another?
- Check for existing documents/embedded documents in MongoDB
- Updating Nested Embedded Documents in MongoDB?
- Aggregate based on array value to sum values in different MongoDB documents?
- Filter query on array of embedded document with MongoDB?
- Querying an array of arrays in MongoDB?
- Querying internal array size in MongoDB?
- Querying array elements with MongoDB?
- How can I find documents in MongoDB based on the number of matched objects within an array?
- Count unique items in array-based fields across all MongoDB documents?
- Get range of months from array based on another array JavaScript
- Get all embedded documents with “isMarried” status in a MongoDB collection

Advertisements