Updated May 8, 2023
Introduction to MongoDB $elemMatch
The $elemMatch Operator of the MongoDB is used to find documents with at least one array field. The finding operation matches the data in the array field with the criteria mentioned with the $elemMatch. $elemMatch is an operator by MongoDB that is used to match elements in an array. MongoDB provides various operators for various operations, and $elemMatch seems to one of the underrated operators.
Sample code:
db.collectionname.find({"field": {$elemMatch: { $gte:11}}})
Such operators in MongoDB provides a great ability for the user to store and manage the data inside the documents.
Syntax with parameters:
{ <field>: { $elemMatch : { <query1>, <query2>, ... } } }
Breaking down the above syntax, starting with the field, which is the key, followed by the keyword $elemMatch and the selection criteria. Then, the query can be any basic comparison operator like equal to or greater than, etc. These comparison operators will filter out the expected result.
How MongoDB $elemMatch Operator works in MongoDB?
- The $elemMatch operator is a simple search and filter operator, that looks for a passed value to be present in an array.
- When we intend to find a document with specific criteria, we cannot use the same find method to work with arrays. Along with arrays, the $elemMatch operator comes in. The $elemMatch operator will search for the passed value inside the array of the documents, and when it matches, it will return the documents.
- If the mentioned criteria match with no record or document present in the collection, it will return a blank. And if we execute this operator without a proper understanding of the array, or over an array with a massive amount of values, then it will result in high usage of CPU and get into doing scans over mini documents.
- Now, two things are recommended not to use with $elemMatch operator. It is not a good practice to define $where operator along with the $elemMatch operator. And the $text operator should also not be used with the $elemMatch. It is an advantage of MongoDB being rich with its query language that we can access a document or records by its array values.
Examples of MongoDB $elemMatch
Given below are the examples of MongoDB $elemMatch:
Below is the screenshot of the collection, fun, which has multiple types of documents, within which are documents with array data types.
Example #1
Code:
db.fun.find({"results": {$elemMatch: {$gte: 81}}})
This query will find values, with the key of results, using the $elemMatch operator. Here $elemMatch operator will search for the values which are greater than equal to 81. We have used another operator here, which is $gte, which is used to filter the values and have the final result to be “Greater Than Equal To”.
So the first example is to find a value in the array, which is greater than equal to 81.
Output:
Above screenshot captures the proper output for the query 1. Our query has returned an array of values upon execution, which consists of a value greater than 81.
Example #2
We will add another comparison operator to the query.
Code:
db.fun.find({"results": {$elemMatch: {$gte: 41,$lt: 66}}})
Our second query extends the first one but adding another layer of filter here. It executes a find method on the fun collection, with $elemMatch operator, along with two query filters. We implemented a $gte operator in our first example. Like $gte, $lt is another operator that filters the records or the values to be “less than”.
Output:
As we can see, our second query has returned several records. These records consist of an array with values greater than 41 and less than 66. There are documents with values larger than 66, like the last one with a value of 77, but we must consider that it contains the value which we intend to find, which is in the same record.
Example #3
For the third example, we will implement the $elemMatch operator over a string array. Until now we have successfully executed $elemMatch operator over an array of numbers, and the outputs are as expected.
Firstly, we have inserted a few records with array values of string.
As shown, we have added a few cities and states names, as an array of string for few records. We will now execute our $elemMatch operator over these records.
Code:
db.fun.find({"results" :{ $elemMatch: { $eq : "Pune"}}})
Breaking down the above query, it is a simple find method over the fun collection, with $elemMatch operator for better filtering. Inside $elemMatch operator, we have a simple equation to the operator, which is $eq. This $eq operator will search and return every document or records that I collected with the value of Pune, inside the array.
Upon execution, this query will return records with Pune as a value in an array.
Output:
As shown in the above screenshot, our query has been successful. It has retrieved every record that consisted of an array of string with the value of Pune. We implemented the $elemMatch operator with an array that includes numeric value and later with string values. In both cases, the working of $elemMatch was similar.
Conclusion
To conclude, MongoDB provides a wide range of operators, out of which $elemMatch is used to search and filter values, over an array. Based on the search criteria or the query, it will search for the matching records. Along with explanation, we implemented the $elemMatch operator over an array of numbers and then on an array of string too. The $elemMatch operator is highly recommended when in need of search operation. Every time we need to look inside an array, we use $elemMatch.
Recommended Articles
This is a guide to MongoDB $elemMatch. Here we discuss the introduction, how MongoDB $elemMatch operator works in MongoDB? and examples respectively. You may also have a look at the following articles to learn more –