- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find MongoDB documents where all objects in array have specific value?
Let us create a collection with documents −
> db.demo74.insertOne( ... { ... StudentName: "Chris", ... StudentDetails: [{ ... "Subject": "MongoDB", ... "isRegular": "Active" ... },{ ... "Subject": "MongoDB", ... "isRegular": "InActive" ... },{ ... "Subject": "MongoDB", ... "isRegular": "InActive" ... }] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e29c6b671bf0181ecc4226f") } > db.demo74.insertOne({ ... name: "document2", ... data: [{ ... "Subject": "MongoDB", ... "isRegular": "Active" ... },{ ... "Subject": "MongoDB", ... "isRegular": "Active" ... },{ ... "Subject": "MongoDB", ... "isRegular": "Active" ... }] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e29c6b771bf0181ecc42270") }
Display all documents from a collection with the help of find() method −
> db.demo74.find();
This will produce the following output −
{ "_id" : ObjectId("5e29c6b671bf0181ecc4226f"), "StudentName" : "Chris", "StudentDetails" : [ { "Subject" : "MongoDB", "isRegular" : "Active" }, { "Subject" : "MongoDB", "isRegular" : "InActive" }, { "Subject" : "MongoDB", "isRegular" : "InActive" } ] } { "_id" : ObjectId("5e29c6b771bf0181ecc42270"), "name" : "document2", "data" : [ { "Subject" : "MongoDB", "isRegular" : "Active" }, { "Subject" : "MongoDB", "isRegular" : "Active" }, { "Subject" : "MongoDB", "isRegular" : "Active" } ] }
Following is the query to find documents where all objects in array have specific value −
> db.demo74.find({ " StudentDetails": { "$not": { "$elemMatch": { "isRegular": { $ne: "Active" } } } }, "StudentDetails.isRegular": "Active" }).pretty();
This will produce the following output −
{ "_id" : ObjectId("5e29c6b671bf0181ecc4226f"), "StudentName" : "Chris", "StudentDetails" : [ { "Subject" : "MongoDB", "isRegular" : "Active" }, { "Subject" : "MongoDB", "isRegular" : "InActive" }, { "Subject" : "MongoDB", "isRegular" : "InActive" } ] }
- Related Articles
- Find documents where all elements of an array have a specific value in MongoDB?
- Find MongoDB documents where elements of an array have a specific value?
- Find all documents that have two specific id's in an array of objects in MongoDB?
- MongoDB query for documents whose array elements does not have a specific value
- Find value above a specific value in MongoDB documents?
- Querying on an array of objects for specific nested documents with MongoDB?
- MongoDB query to get specific list of names from documents where the value of a field is an array
- How to select objects where an array contains only a specific field in MongoDB?
- Update objects in a MongoDB documents array (nested updating)?
- Get MongoDB documents that contains specific attributes in array
- MongoDB query to match documents with array values greater than a specific value
- Find documents that contains specific field in MongoDB?
- MongoDB query to update all documents matching specific IDs
- Find specific key value in array of objects using JavaScript
- How to find latest entries in array over all MongoDB documents?

Advertisements