
- 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 can I find documents in MongoDB based on the number of matched objects within an array?
Let us see an example and create a collection with documents −
> db.demo694.insertOne( ... { ... "details" : ... [ ... { ... "Name" : "Chris", ... Age:21 ... }, ... { ... "Name" : "David", ... Age:22 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea59279ece4e5779399c07f") } > db.demo694.insertOne( ... { ... "details" : ... [ ... { ... "Name" : "Bob", ... Age:21 ... }, ... { ... "Name" : "Mike", ... Age:23 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea5927aece4e5779399c080") } > db.demo694.insertOne( ... { ... "details" : ... [ ... { ... "Name" : "Chris", ... Age:21 ... }, ... { ... "Name" : "Carol", ... Age:22 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea5927aece4e5779399c081") }
Display all documents from a collection with the help of find() method −
> db.demo694.find();
This will produce the following output −
{ "_id" : ObjectId("5ea59279ece4e5779399c07f"), "details" : [ { "Name" : "Chris", "Age" : 21 }, { "Name" : "David", "Age" : 22 } ] } { "_id" : ObjectId("5ea5927aece4e5779399c080"), "details" : [ { "Name" : "Bob", "Age" : 21 }, { "Name" : "Mike", "Age" : 23 } ] } { "_id" : ObjectId("5ea5927aece4e5779399c081"), "details" : [ { "Name" : "Chris", "Age" : 21 }, { "Name" : "Carol", "Age" : 22 } ] }
Following is the query to find documents in MongoDB based on the number of matched objects within an array −
> nameChrisAge21= function () { ... var inc = 0; ... this.details.forEach(function (d) { ... if (d.Name == "Chris" && d.Age == 21) { ... inc= inc + 1; ... } ... }); ... if (inc >= 1) { ... return true; ... } ... } function () { var inc = 0; this.details.forEach(function (d) { if (d.Name == "Chris" && d.Age == 21) { inc= inc + 1; } }); if (inc >= 1) { return true; } } > db.demo694.find({$where:nameChrisAge21});
This will produce the following output −
{ "_id" : ObjectId("5ea59279ece4e5779399c07f"), "details" : [ { "Name" : "Chris", "Age" : 21 }, { "Name" : "David", "Age" : 22 } ] } { "_id" : ObjectId("5ea5927aece4e5779399c081"), "details" : [ { "Name" : "Chris", "Age" : 21 }, { "Name" : "Carol", "Age" : 22 } ] }
- Related Articles
- How to filter documents based on an array in MongoDB?
- How can I count the documents in an array based on the value of a specific field?
- Querying array of Embedded Documents in MongoDB based on Range?
- Querying on an array of objects for specific nested documents with MongoDB?
- Update an array of strings nested within an array of objects in MongoDB
- How to use MongoDB $pull to delete documents within an Array?
- Creating an array of objects based on another array of objects JavaScript
- Find by _id on an array of objects in MongoDB database?
- Aggregate based on array value to sum values in different MongoDB documents?
- Find MongoDB documents where all objects in array have specific value?
- Can we search an array of objects in MongoDB?
- Filter an array containing objects based on another array containing objects in JavaScript
- Find all documents that have two specific id's in an array of objects in MongoDB?
- Find result within array of objects and match email address field in MongoDB?
- How to sort an array of objects based on the length of a nested array in JavaScript

Advertisements