Elemmatch Query in MongoDB: Simple Guide

Elemmatch Query Featured Image

In this tutorial, I am going to talk about the elemmatch query operator in MongoDB.

The MongoDB database provides a variety of operators, so you can choose the one that is best suited to your needs. Thanks to its very powerful, simple-to-use, and flexible operators, this database management system service allow you to build applications capable of performing far more than CRUD operations. Applications benefit from the great power of MongoDB, which has been popular for quite some time now.

As well as providing the ability to query, print, and update data, MongoDB also supports a variety of operators. Because the interface is so simple to use, everyone can use it regardless of their experience level.

Among MongoDB’s, the most common and useful operator is the elemmatch query operator also written as the $elemMatch operator, which I’ll talk about today.

The elemmatch query operator allows us to match documents with an array field that contains at least one element that matches the specified query criteria. As a rule of thumb, MongoDB doesn’t allow you to use the $where operator and the $text operator with the elemmatch query operator.

In this guide, I will demonstrate a few examples for you so you can get a good idea of how you can use the elemmatch query operator for your MongoDB-backed applications in the future.

So, let us get going.

The elemmatch Query Operator Syntax

{ : {$elemMatch: { query1, query2, ...} } }

Using the elemmatch Query Operator to Match an Element in MongoDB

Let us start to use the MongoDB elemmatch query operator by using it to match an element with a specified query.

  • Start up the MongoDB server
  • Pick the database you want to use and move into it:
show dbs
use testGrades
  • Let us take a quick look at all of the documents in our grades collection first:
> db.grades.find({})

{ _id: 1, name: "John Mendes", grades: [ 82, 85, 88, 90, 78 ] }

{ _id: 2, name: "Sofia Derulo", grades: [ 70, 75, 68, 65, 70 ] }

{ _id: 3, name: "Areesha Shaikh", grades: [ 86, 89, 93, 90, 88 ] }

{ _id: 4, name: "Grace Handles", grades: [ 55, 65, 63, 68, 70 ] }

{ _id: 5, name: "Rebecca Chism", grades: [ 82, 78, 80, 91, 75 ] }

{ _id: 6, name: "David Hudson", grades: [ 67, 70, 78, 74, 72 ] }

{ _id: 7, name: "Latonya Jacobson", grades: [ 80, 87, 84, 95, 81 ] }

{ _id: 8, name: "Luigi Markham", grades: [ 82, 73, 79, 80, 71 ] }
  • Next, let us use the elemmatch query operator to match array fields with a given value to find out the nerds of the class:
db.grades.find( { grades: { $elemMatch: { $gte: 86, $lt: 100 } } } )

{ _id: 1, name: "John Mendes", grades: [ 82, 85, 88, 90, 78 ] }

{ _id: 3, name: "Areesha Shaikh", grades: [ 86, 89, 93, 90, 88 ] }

{ _id: 7, name: "Latonya Jacobson", grades: [ 80, 87, 84, 95, 81 ] }

Using elemmatch Query Operator to Match Values with Embedded Array Fields

Let us grab an example from w3resource and understand how we can use the elemmatch query operator to match values with embedded or nested array fields in documents.

So, here’s what the w3resouce sample data looks like:

{
        "_id" : ObjectId("5285bd678154c4747b705b4f"),
        "item_code" : "I001",
        "category" : [
                "boy",
                "girl"
        ],
        "description" : [
                {
                        "agegroup" : "3-5",
                        "flavour" : "chocolate",
                        "price" : 5
                },
                {
                        "agegroup" : "6-9",
                        "flavour" : "strawberry",
                        "price" : 6
                },
                {
                        "agegroup" : "10-13",
                        "flavour" : "mango",
                        "price" : 7
                }
        ]
}
{
        "_id" : ObjectId("5285bd808154c4747b705b50"),
        "item_code" : "I002",
        "category" : [
                "boy",
                "girl"
        ],
        "description" : [
                {
                        "agegroup" : "3-5",
                        "flavour" : "vanilla",
                        "price" : 3
                },
                {
                        "agegroup" : "6-9",
                        "flavour" : "lemon",
                        "price" : 6
                },
                {
                        "agegroup" : "10-13",
                        "flavour" : "mango",
                        "price" : 5
                }
        ]
}
{
        "_id" : ObjectId("5285bd8a8154c4747b705b51"),
        "item_code" : "I003",
        "category" : [
                "boy",
                "girl"
        ],
        "description" : [
                {
                        "agegroup" : "3-5",
                        "flavour" : "pineapple",
                        "price" : 5
                },
                {
                        "agegroup" : "6-9",
                        "flavour" : "mango",
                        "price" : 6
                },
                {
                        "agegroup" : "10-13",
                        "flavour" : "vanilla",
                        "price" : 5
                }
        ]
}
  • Now, we will use the elemmatch query operator in MongoDB to match values with nested array fields:
> db.table1.find( { "description": { $elemMatch: { "agegroup" : "3-5","price" : 5}}}).pretty();

{
        "_id" : ObjectId("5285bd678154c4747b705b4f"),
        "item_code" : "I001",
        "category" : [
                "boy",
                "girl"
        ],
        "description" : [
                {
                        "agegroup" : "3-5",
                        "flavour" : "chocolate",
                        "price" : 5
                },
                {
                        "agegroup" : "6-9",
                        "flavour" : "strawberry",
                        "price" : 6
                },
                {
                        "agegroup" : "10-13",
                        "flavour" : "mango",
                        "price" : 7
                }
        ]
}
{
        "_id" : ObjectId("5285bd8a8154c4747b705b51"),
        "item_code" : "I003",
        "category" : [
                "boy",
                "girl"
        ],
        "description" : [
                {
                        "agegroup" : "3-5",
                        "flavour" : "pineapple",
                        "price" : 5
                },
                {
                        "agegroup" : "6-9",
                        "flavour" : "mango",
                        "price" : 6
                },
                {
                        "agegroup" : "10-13",
                        "flavour" : "vanilla",
                        "price" : 5
                }
        ]
}

This way, we have successfully learned how to use the elemmatch query operator in MongoDB.

Read More: $each Operator in MongoDB

Conclusion

The MongoDB database provides a variety of operators, so you can choose the one that is best suited to your needs. Thanks to its very powerful, simple-to-use, and flexible operators, this database management system service allow you to build applications capable of performing far more than CRUD operations.

In this guide, I have demonstrated a few examples for you so you can get a good idea on how you can use the elemmatch query operator for your MongoDB-backed applications in the future.

Noteworthy References

W3Resource

MongoDB Docs