i have document as given below:
{
"_id" : ObjectId("999999b64655dea5e296de31b00"),
"companies" : [
{
"companyId" : "ABC",
"range" : {
"start" : 0,
"end" : 30
}
},
{
"companyId" : "DEF",
"range" : {
"start" : 20,
"end" : 30
}
},
{
"companyId" : "CGH",
"range" : {
"start" : 40,
"end" : 50
}
},
{
"companyId" : "ABC",
"range" : {
"start" : 60,
"end" : 90
}
},
],
"createdTs" : NumberLong(1612407909406),
"updatedTs" : NumberLong(1620891030125)
}
How to find "companyId" : "ABC"
and return those elements only using $project
?
I able to return the list using
db.getCollection('companyDetails').aggregate([
{$match: {"companies.companyId": "ABC"}},
{$project: {
list: {$filter: {
input: "$companies",
as: "item",
cond: {$eq:['$$item.companyId', 'ABC']}
}}
}}
]);
output :
{
"_id" : ObjectId("999999b64655dea5e296de31b00"),
"list" : [
{
"companyId" : "ABC",
"range" : {
"start" : 0,
"end" : 30
}
},
{
"companyId" : "ABC",
"range" : {
"start" : 60,
"end" : 90
}
}
]
}
But . i need list of range only instead of whole block
example: expected
{
"_id" : ObjectId("999999b64655dea5e296de31b00"),
"list" : [
"range" : {
"start" : 0,
"end" : 30
},
"range" : {
"start" : 60,
"end" : 90
}
]
}