

- 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 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 Questions & Answers
- 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 on an array of objects for specific nested documents with MongoDB?
- Querying array of Embedded Documents in MongoDB based on Range?
- Creating an array of objects based on another array of objects JavaScript
- Update an array of strings nested within an array of objects in MongoDB
- Find by _id on an array of objects in MongoDB database?
- How to use MongoDB $pull to delete documents within an Array?
- Can we search an array of objects in MongoDB?
- Filter an array containing objects based on another array containing objects in JavaScript
- How can I aggregate nested documents in MongoDB?
- Find MongoDB documents where all objects in array have specific value?
- How to sort an array of objects based on the length of a nested array in JavaScript
- Find result within array of objects and match email address field in MongoDB?
- How do I display a list of objects based on a specific property with MongoDB?
Advertisements