
- 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 query for documents matching array, irrespective of elements order
For this, use $all in MongoDB. The $all operator in MongoDB selects the documents where the value of a field is an array that contains all the specified elements.
Let us first create a collection with documents −
> db.demo370.insertOne( ... { ... "Name" : "Chris", ... "details" : [ ... { ... "Subject" : "MySQL", ... "CountryName" : "US" ... }, ... { ... "Subject" : "MongoDB", ... "CountryName" : "UK" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e57e9892ae06a1609a00af0") } > db.demo370.insertOne( ... { ... "Name" : "David", ... "details" : [ ... { "Subject" : "Java", ... "CountryName" : "AUS" ... }, ... { ... "Subject" : "Spring", ... "CountryName" : "UK" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e57e9972ae06a1609a00af1") }
Display all documents from a collection with the help of find() method −
> db.demo370.find();
This will produce the following output −
{ "_id" : ObjectId("5e57e9892ae06a1609a00af0"), "Name" : "Chris", "details" : [ { "Subject" : "MySQL", "CountryName" : "US" }, { "Subject" : "MongoDB", "CountryName" : "UK" } ] } { "_id" : ObjectId("5e57e9972ae06a1609a00af1"), "Name" : "David", "details" : [ { "Subject" : "Java", "CountryName" : "AUS" }, { "Subject" : "Spring", "CountryName" : "UK" } ] }
Following is the query for documents matching array, irrespective of elements order −
> db.demo370.find({ ... details: {$all: [ ... {Subject:"MySQL","CountryName" : "US"}, ... {Subject:"MongoDB", "CountryName" : "UK"} ... ]}})
This will produce the following output −
{ "_id" : ObjectId("5e57e9892ae06a1609a00af0"), "Name" : "Chris", "details" : [ { "Subject" : "MySQL", "CountryName" : "US" }, { "Subject" : "MongoDB", "CountryName" : "UK" } ] }
- Related Articles
- MongoDB query to find matching documents given an array with values?
- MongoDB query to display documents with a specific name irrespective of case
- MongoDB query to change order of array elements?
- How to specify the order in which the query returns matching documents in MongoDB
- MongoDB query to update all documents matching specific IDs
- MongoDB query for documents whose array elements does not have a specific value
- Search for documents matching first item in an array with MongoDB?
- Query for documents where array size is greater than 1 in MongoDB?
- MongoDB query to select 10 most recent documents without changing order?
- MongoDB - Query embedded documents?
- Query an array of embedded documents in MongoDB and push another?
- MongoDB query to match documents that contain an array field
- MongoDB query to skip documents
- MongoDB exact array matching
- MongoDB query for array concatenation?

Advertisements