w3resource

MongoDB Projection operator - $elemMatch

Description

In MongoDB, the $elemMatch projection operator is used to limits the contents of an array field which is included in the query results to contain only the first matching element in the array, according to the specified condition.

Array elements are treated as a document.

If multiple elements match with the $elemMatch operator then only the first matching element are eligible to appear in the result.

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

Sample collection "mediitem2"

{
        "_id" : 1,
        "batch" : 10452,
        "tran_details" : [
                {
                        "qty" : 200,
                        "prate" : 50,
                        "mrp" : 70
                },
                {
                        "qty" : 250,
                        "prate" : 50,
                        "mrp" : 60
                },
                {
                        "qty" : 190,
                        "prate" : 55,
                        "mrp" : 75
                }
        ]
}
{
        "_id" : 2,
        "batch" : 73292,
        "tran_details" : [
                {
                        "qty" : 210,
                        "prate" : 54,
                        "mrp" : 64
                },
                {
                        "qty" : 225,
                        "prate" : 60,
                        "mrp" : 68
                }
        ]
}
{
        "_id" : 3,
        "batch" : 10452,
        "tran_details" : [
                {
                        "qty" : 185,
                        "prate" : 57,
                        "mrp" : 68
                },
                {
                        "qty" : 200,
                        "prate" : 57,
                        "mrp" : 65
                }
        ]
}
{
        "_id" : 4,
        "batch" : 10452,
        "tran_details" : [
                {
                        "qty" : 185,
                        "prate" : 50,
                        "mrp" : 68
                }
        ]
}

Example 1

The following mongodb find() operation queries all documents from the collection where the value of the batch field is 10452. The $elemMatch projection returns only the first matching element (although it matches more than one elements) of the tran_details array where the prate field has a value of 50.

 db.mediitem2.find( { batch: 10452 },{ tran_details: { $elemMatch: { prate: 50 } } } ).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 of the command:

{
        "_id" : 1,
        "tran_details" : [
                {
                        "qty" : 200,
                        "prate" : 50,
                        "mrp" : 70
                }
        ]
}
{ "_id" : 3 }
{
        "_id" : 4,
        "tran_details" : [
                {
                        "qty" : 185,
                        "prate" : 50,
                        "mrp" : 68
                }
        ]
}

Here in the above example-

  • for the document with _id equal to 1, the tran_details array contains multiple elements with the prate field equal to 50. However, the $elemMatch projection returns only the first matching element from the array.
  • The document with _id equal to 3 does not contain the tran_details field in the result, that no element are matching with the $elemMatch condition.
  • and, for the document with _id equal to 4, the tran_details array contains only the elements with the prate field equal to 50.

Example 2

The following find() operation queries for all documents where the value of the batch field is 10452. The projection includes the first matching element of the tran_details array where the prate field has a value of 50 and the mrp field is greater than equal to 70.

> db.mediitem2.find({batch: 10452 },{tran_details: {$elemMatch: {prate: 50, mrp: {$gte: 70}}}}).pretty();

 

Output of the command:

{
        "_id" : 1,
        "tran_details" : [
                {
                        "qty" : 200,
                        "prate" : 50,
                        "mrp" : 70
                }
        ]
}
{ "_id" : 3 }
{ "_id" : 4 }

Here in the above example-

  • for the document with _id equal to 1, the tran_details array contains multiple elements with the prate field equal to 50 and mrp greater than equal to 70. However, the $elemMatch projection returns only the first matching element from the array.
  • The document with _id equal to 3 and 4 does not contain the tran_details field in the result, that no element are matching with the $elemMatch condition.

Example 3 : MongoDB Projection operator - $elemMatch with sort

When an array field contains multiple documents with the same field name then a sort() method along with the find() method can be used to arrange the repeating field in a specific order.

The following mongodb find() operation queries all documents from the collection where the value of the batch field is 10452. The $elemMatch projection returns only the first matching element (although it matches more than one elements) of the tran_details array where the prate field has a value of 50 and the result set will arrange in ascending order according to the or qty field.

> db.mediitem2.find({ batch: 10452 },{ tran_details: { $elemMatch: { prate: 50 }}}).sort({"tran_details.qty":1}).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 of the command:

{ "_id" : 3 }
{
        "_id" : 4,
        "tran_details" : [
                {
                        "qty" : 185,
                        "prate" : 50,
                        "mrp" : 68
                }
        ]
}
{
        "_id" : 1,
        "tran_details" : [
                {
                        "qty" : 200,
                        "prate" : 50,
                        "mrp" : 70
                }
        ]
}

Previous: $
Next: $slice



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mongodb/mongodb-elemmatch-projection-operators.php