- 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
How do I work with array fields in MongoDB to match all?
To match all in MongoDB, use $all. The $all operator selects the documents where the value of a field is an array that contains all the specified elements. Let us create a collection with documents −
> db.demo695.insertOne({"ListOfValues":[100,200,500,800]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6d4c4551299a9f98c938f") } > db.demo695.insertOne({"ListOfValues":[1000,200,4000]}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6d4cf551299a9f98c9390") }
Display all documents from a collection with the help of find() method −
> db.demo695.find();
This will produce the following output −
{ "_id" : ObjectId("5ea6d4c4551299a9f98c938f"), "ListOfValues" : [ 100, 200, 500, 800 ] } { "_id" : ObjectId("5ea6d4cf551299a9f98c9390"), "ListOfValues" : [ 1000, 200, 4000 ] }
Following is the query to work with array field and match all −
> db.demo695.find({"ListOfValues":{$all:[1000,200,4000]}});
This will produce the following output −
{ "_id" : ObjectId("5ea6d4cf551299a9f98c9390"), "ListOfValues" : [ 1000, 200, 4000 ] }
- Related Articles
- How do I use MongoDB to count only collections that match two fields?
- Match MongoDB documents with fields not containing values in array?
- How do I index “or” in MongoDB for indexing multiple fields?
- How do I search according to fields in inner classes using MongoDB db.coll.find()?
- Match between fields in MongoDB aggregation framework?
- Sort array in MongoDB query and project all fields?
- How to match date with MongoDB $match?
- Group all documents with common fields in MongoDB?
- How to match multiple criteria inside an array with MongoDB?
- Working with Aggregation to match all the values in MongoDB
- How to group nested fields in MongoDB aggregation with count value in array?
- The collection.find() always returns all fields with MongoDB?
- How to find datatype of all the fields in MongoDB?
- Count unique items in array-based fields across all MongoDB documents?
- How do I push elements to an existing array in MongoDB?

Advertisements