
- 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
MongoDB find by multiple array items?
You can use $all operator to find by multiple array items. To understand the concept, let us create a collection with the document.
The query to create a collection with a document is as follows −
> db.findByMultipleArrayDemo.insertOne({"StudentFirstName":"John","StudentLastName":"Smith", "StudentCoreSubject":["Compiler","Operating System","Computer Networks"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c7ef07b559dd2396bcfbfc4") } > db.findByMultipleArrayDemo.insertOne({"StudentFirstName":"Carol","StudentLastName":"Taylor", "StudentCoreSubject":["MongoDB","MySQL","SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c7ef09d559dd2396bcfbfc5") } > db.findByMultipleArrayDemo.insertOne({"StudentFirstName":"Bob","StudentLastName":"Taylor", "StudentCoreSubject":["MongoDB","MySQL","SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c7ef0c7559dd2396bcfbfc6") } > db.findByMultipleArrayDemo.insertOne({"StudentFirstName":"David","StudentLastName":"Johnson", "StudentCoreSubject":["Compiler","Operating System","Computer Networks"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c7ef0f2559dd2396bcfbfc7") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.findByMultipleArrayDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c7ef07b559dd2396bcfbfc4"), "StudentFirstName" : "John", "StudentLastName" : "Smith", "StudentCoreSubject" : [ "Compiler", "Operating System", "Computer Networks" ] } { "_id" : ObjectId("5c7ef09d559dd2396bcfbfc5"), "StudentFirstName" : "Carol", "StudentLastName" : "Taylor", "StudentCoreSubject" : [ "MongoDB", "MySQL", "SQL Server" ] } { "_id" : ObjectId("5c7ef0c7559dd2396bcfbfc6"), "StudentFirstName" : "Bob", "StudentLastName" : "Taylor", "StudentCoreSubject" : [ "MongoDB", "MySQL", "SQL Server" ] } { "_id" : ObjectId("5c7ef0f2559dd2396bcfbfc7"), "StudentFirstName" : "David", "StudentLastName" : "Johnson", "StudentCoreSubject" : [ "Compiler", "Operating System", "Computer Networks" ] }
Here is the query to find by multiple array items −
> db.findByMultipleArrayDemo.find({ StudentCoreSubject: { $all: ["Compiler", "Computer Networks"] }}).pretty();
The following is the output displaying record with array items "Compiler" and "Computer Networks −
{ "_id" : ObjectId("5c7ef07b559dd2396bcfbfc4"), "StudentFirstName" : "John", "StudentLastName" : "Smith", "StudentCoreSubject" : [ "Compiler", "Operating System", "Computer Networks" ] } { "_id" : ObjectId("5c7ef0f2559dd2396bcfbfc7"), "StudentFirstName" : "David", "StudentLastName" : "Johnson", "StudentCoreSubject" : [ "Compiler", "Operating System", "Computer Networks" ] }
- Related Articles
- MongoDB find by multiple array items using $in?
- Query to retrieve multiple items in an array in MongoDB?
- Find value in a MongoDB Array with multiple criteria?
- Matching MongoDB collection items by id?
- Clearing items in a nested MongoDB array?
- Get array items inside a MongoDB document?
- Implement $dateToString on array items with MongoDB
- MongoDB query to find multiple matchings inside array of objects?
- Filtering MongoDB items by fields and subfields?
- How to count items in array with MongoDB?
- MongoDB query to find value in array with multiple criteria (range)
- MongoDB projection result as an array of selected items?
- Count by multiple fields with MongoDB aggregation
- How to find elements of JavaScript array by multiple values?
- Pull multiple objects from an array in MongoDB?

Advertisements