MongoDb - Operator Array - $elemMatch

Description

The MongoDB $elemMatch operator matches more than one component within an array element.

Our database name is 'myinfo' and our collection name is 'table1'. Here, is the collection bellow.

Sample collection "table1"

{
        "_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
                }
        ]
}

Example - 1 of MongoDB Evaluation Query operator - $elemMatch

If we want to select all documents from the collection "table1" which satisfying the conditions in the $elemMatch expression in array description -

The agegroup must be within "3-5"

The price must be equal to 5

the following mongodb command can be used :

>db.table1.find( { "description": { $elemMatch: { "agegroup" : "3-5","price" : 5}}}).pretty();

N.B. find() method displays the documents in a non structured format but to display the results in a formatted way, the pretty() method can be used.

Output

{
        "_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
                }
        ]
}

Example - 2 of MongoDB Evaluation Query operator - $elemMatch

If we want to select all documents from the collection "table1" which satisfying the conditions in the $elemMatch expression in array description -

The agegroup must be within "10-13"

The price must be more than or equal to 7

the following mongodb command can be used :

>db.table1.find( { "description": { $elemMatch: { "agegroup" : "10-13","price":{$gte:7}}}}).pretty();

N.B. find() method displays the documents in a non structured format but to display the results in a formatted way, the pretty() method can be used.

Output

{
        "_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
                }
        ]
}

? Source: http://www.w3resource.com/mongodb/mongodb-elemmatch-operators.php