
- 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
How do you perform an AND query on an array in MongoDB?
To get the same result like AND in MongoDB, use the $all operator. Let us first create a collection with documents −
> db.andQueryDemo.insertOne({"StudentName":"Carol Taylor","FavouriteSubject":["C","Java","MongoDB","MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cc73e7a8f9e6ff3eb0ce433") } > db.andQueryDemo.insertOne({"StudentName":"David Miller","FavouriteSubject":["C++","Java","MongoDB","SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cc73ea48f9e6ff3eb0ce434") } > db.andQueryDemo.insertOne({"StudentName":"Carol Taylor","FavouriteSubject":["Python","PL/SQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cc73ed38f9e6ff3eb0ce435") }
Following is the query to display all documents from a collection with the help of find() method −
> db.andQueryDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cc73e7a8f9e6ff3eb0ce433"), "StudentName" : "Carol Taylor", "FavouriteSubject" : [ "C", "Java", "MongoDB", "MySQL" ] } { "_id" : ObjectId("5cc73ea48f9e6ff3eb0ce434"), "StudentName" : "David Miller", "FavouriteSubject" : [ "C++", "Java", "MongoDB", "SQL Server" ] } { "_id" : ObjectId("5cc73ed38f9e6ff3eb0ce435"), "StudentName" : "Carol Taylor", "FavouriteSubject" : [ "Python", "PL/SQL" ] }
Following is the query to do an AND query on an array in MongoDB with $all. Here, we are displaying the array with both “Java” and “MongoDB” as FavouriteSubject −
> db.andQueryDemo.find({FavouriteSubject:{$all:["Java","MongoDB"]}}).pretty();
This will produce the following output −
{ "_id" : ObjectId("5cc73e7a8f9e6ff3eb0ce433"), "StudentName" : "Carol Taylor", "FavouriteSubject" : [ "C", "Java", "MongoDB", "MySQL" ] } { "_id" : ObjectId("5cc73ea48f9e6ff3eb0ce434"), "StudentName" : "David Miller", "FavouriteSubject" : [ "C++", "Java", "MongoDB", "SQL Server" ] }
- Related Articles
- How do you limit an array sub-element in MongoDB?
- How do you remove an array element by its index in MongoDB
- MongoDB query to access an object in an array
- Query on the last object of an array with MongoDB
- How to perform heapsort on an array in Java?
- How do you empty an array in C#?
- How do you make an array in Python?
- Query in MongoDB to perform an operation similar to LIKE operation
- How do you convert an ArrayList to an array in Java?
- MongoDB query for Partial Object in an array
- MongoDB query to replace value in an array?
- How to perform binary search on an array in java?
- Query an array of embedded documents in MongoDB and push another?
- MongoDB query to match and remove element from an array?
- MongoDB query for capped sub-collection in an array

Advertisements