In the following entries, I want to get item_details, where the type is " system " and the id is " 7634e9639f5c25f2434b72d8a "
var operator = {
'item_details': {
$elemMatch: {
type: 'custom'
}
}
}
var query = {
_id : "7634e9639f5c25f2434b72d8a"
};
req.db.collection('products').find(query, operator).toArray(function (err, result) {
if(err)throw err;
console.log(result);
});
It returns only the first element. But you need to get all the records that match the query and projection. The following are sample data from which I want to execute a query.
[
{
"_id": "7634e9639f5c25f2434b72d8a",
"item_details": [
{
"quantity": 1,
"sub_total": 1201,
"type": "system"
},
{
"quantity": 19,
"sub_total": 140,
"type": "custom"
},
{
"quantity": 81,
"sub_total": 130,
"type": "custom"
},
{
"quantity": 71,
"sub_total": 90,
"type": "system"
}
]
},
{
"_id": "564e9639f5c25f2434b72d8a",
"item_details": [
{
"quantity": 1,
"sub_total": 101,
"type": "system"
},
{
"quantity": 9,
"sub_total": 40,
"type": "custom"
},
{
"quantity": 8,
"sub_total": 30,
"type": "custom"
},
{
"quantity": 7,
"sub_total": 60,
"type": "system"
}
]
}
]
Rohit source
share