
- 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 to find exact Array Match with values in different order using MongoDB?
To find exact array match with values in different order, you can use $all operator. Let us create a collection with documents. Following is the query
>db.exactMatchArrayDemo.insertOne({"StudentName":"David","StudentAge":22,"StudentGameScores":[45,78,98]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c94702d6669774125246c") } >db.exactMatchArrayDemo.insertOne({"StudentName":"Chris","StudentAge":23,"StudentGameScores":[45,78]}); { "acknowledged" : true, "insertedId" : ObjectId("5c9c94a42d6669774125246d") }
Following is the query to display all documents from a collection with the help of find() method
> db.exactMatchArrayDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9c94702d6669774125246c"), "StudentName" : "David", "StudentAge" : 22, "StudentGameScores" : [ 45, 78, 98 ] } { "_id" : ObjectId("5c9c94a42d6669774125246d"), "StudentName" : "Chris", "StudentAge" : 23, "StudentGameScores" : [ 45, 78 ] }
Following is the query to find exact array match
> db.exactMatchArrayDemo.find({ "StudentGameScores": { "$size" : 2, "$all": [ 78, 45 ] } }).pretty();
This will produce the following output
{ "_id" : ObjectId("5c9c94a42d6669774125246d"), "StudentName" : "Chris", "StudentAge" : 23, "StudentGameScores" : [ 45, 78 ] }
Following is the query to find exact array match but order does not matter. We have set a different size to get the field “StudentGameScores” with 3 values
> db.exactMatchArrayDemo.find({ "StudentGameScores": { "$size" : 3, "$all": [ 78, 45 ] } }).pretty();
This will produce the following output
{ "_id" : ObjectId("5c9c94702d6669774125246c"), "StudentName" : "David", "StudentAge" : 22, "StudentGameScores" : [ 45, 78, 98 ] }
- Related Articles
- Find the exact match in array without using the $elemMatch operator in MongoDB?
- MongoDB query for exact match
- Match MongoDB documents with fields not containing values in array?
- MongoDB exact array matching
- MongoDB query to match documents with array values greater than a specific value
- How to match date with MongoDB $match?
- How to match multiple criteria inside an array with MongoDB?
- Working with Aggregation to match all the values in MongoDB
- MongoDB query for exact match on multiple document fields
- How do I work with array fields in MongoDB to match all?
- How to specify exact order with WHERE `id` IN (…) in MySql?
- Update field in exact element array in MongoDB?
- Implement array match in MongoDB?
- Coalesce values from different properties into a single array with MongoDB aggregation
- Match multiple criteria inside an array with MongoDB?

Advertisements