- 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 documents where all elements of an array have a specific value in MongoDB?n
You can use find() for this. Let us first create a collection with documents −
> db.findDocumentsDemo.insertOne( { _id: 101, "ProductDetails": [ { "ProductValue":100 }, { "ProductValue":120 } ] } ); { "acknowledged" : true, "insertedId" : 101 } > db.findDocumentsDemo.insertOne( { _id: 102, "ProductDetails": [ { "ProductValue":120}, { "ProductValue":120 }, { "ProductValue":120 } ] } ); { "acknowledged" : true, "insertedId" : 102 }
Following is the query to display all documents from a collection with the help of find() method −
> db.findDocumentsDemo.find().pretty();
This will produce the following output −
{ "_id" : 101, "ProductDetails" : [ { "ProductValue" : 100 }, { "ProductValue" : 120 } ] } { "_id" : 102, "ProductDetails" : [ { "ProductValue" : 120 }, { "ProductValue" : 120 }, { "ProductValue" : 120 } ] }
Following is the query to find documents where all elements of an array have a specific value i.e. ProductValue 120 here −
> db.findDocumentsDemo.find({ "ProductDetails.ProductValue" : { }, "ProductDetails" : { $not : { $elemMatch : { "ProductValue" : { $ne : 120 } } } } });
This will produce the following output −
{ "_id" : 102, "ProductDetails" : [ { "ProductValue" : 120 }, { "ProductValue" : 120 }, { "ProductValue" : 120 } ] }
- 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 MongoDB documents where all objects in array have specific value?
- MongoDB query for documents whose array elements does not have a specific value
- Find all documents that have two specific id's in an array of objects in MongoDB?
- Find value above a specific value in MongoDB documents?
- MongoDB query to get specific list of names from documents where the value of a field is an array
- Get count of array elements from a specific field in MongoDB documents?
- MongoDB query to match documents with array values greater than a specific value
- Querying on an array of objects for specific nested documents with MongoDB?
- Query an array in MongoDB to fetch a specific value
- How can I use MongoDB to find all documents which have a field, regardless of the value of that field?
- Get MongoDB documents that contains specific attributes in array
- MongoDB query to convert an array to a map of documents with n attributes?
- Find documents that contains specific field in MongoDB?
- MongoDB query to update all documents matching specific IDs

Advertisements