MongoDB Projection operator - $
Description
In MongoDB, the positional operator $ is used to limits only the first matching element from an array field, and it happened although there is more than one matching elements in the array.
It must require to appear the <array> field in the query document.
One positional $ operator can only appear in the projection document.
At a time only one array field can appear in the query document;
Syntax:
>db.collection.find( { <array>: <value> ... },{ "<array>.$": 1 } )
or
db.collection.find( { <array.field>: <value> ...}, { "<array>.$": 1 } )
Parameters:
<array> - name of the array
<value> - it may be documents that contains query operator expressions.
field - name of the column or field of the collection.
Our database name is 'myinfo' and our collection name is 'mediitem'. Here, is the collection bellow.
Sample collection "mediitem"
{ "_id" : 1, "batch" : 1, "quantity" : [ 200, 225, 190 ] }
{ "_id" : 2, "batch" : 1, "quantity" : [ 190, 288, 192 ] }
{ "_id" : 3, "batch" : 1, "quantity" : [ 185, 210, 290 ] }
{ "_id" : 4, "batch" : 2, "quantity" : [ 179, 185, 180 ] }
{ "_id" : 5, "batch" : 2, "quantity" : [ 188, 188, 292 ] }
{ "_id" : 6, "batch" : 2, "quantity" : [ 295, 190, 196 ] }
Example : MongoDB Projection operator - $
In the following query, the projection { "quantity.$": 1 } returns only the first element greater than or equal to 180 for the quantity field.
> db.mediitem.find( { batch: 1, quantity: { $gte: 180 } },{ "quantity.$": 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" : 1, "quantity" : [ 200 ] } { "_id" : 2, "quantity" : [ 190 ] } { "_id" : 3, "quantity" : [ 185 ] }
Although the array field quantity contains multiple quantity of elements that are greater than or equal to 180, the $ projection operator returns only the first matching element from the array.
Sample collection "mediitem"
{
"_id" : 1,
"batch" : 1,
"statement" : [
{
"qty" : 200,
"prate" : 50,
"mrp" : 70
},
{
"qty" : 250,
"prate" : 52,
"mrp" : 60
},
{
"qty" : 190,
"prate" : 55,
"mrp" : 75
}
]
}
{
"_id" : 2,
"batch" : 1,
"statement" : [
{
"qty" : 210,
"prate" : 54,
"mrp" : 64
},
{
"qty" : 225,
"prate" : 50,
"mrp" : 65
},
{
"qty" : 195,
"prate" : 60,
"mrp" : 65
}
]
}
Example : MongoDB Projection operator - $ with sort
If we want to return the first element which match the condition qty is greater than 200 for the statement field from the collection mediitem in ascending order the following mongodb query can be used.
>db.mediitem.find( { "statement.qty": { $gt: 200 } },
{ "statement.$": 1 }).sort( { "statement.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.
In the above query, the projection { "statement.$": 1 } returns only the first element with the qty greater than 200 for the statement field. The query also includes a sort() to order by ascending statement.qty field:
Output of the command:
{ "_id" : 2, "statement" : [ { "qty" : 210, "prate" : 54, "mrp" : 64 } ] } { "_id" : 1, "statement" : [ { "qty" : 250, "prate" : 52, "mrp" : 60 } ] }
Previous:
$size
Next:
$elemMatch (projection)
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-$-projection-operators.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics