Hi,
I have the following collection
[
{
"fiedl1": "value1",
"field2": "value2",
"master": [
{
"attributes": {
"key": [
"FIRSTKEY"
]
},
"Status": "ACTIVE"
},
{
"attributes": {
"key": [
"SECONDKEY"
]
},
"Status": "ACTIVE"
},
{
"attributes": {
"key": [
"THIRDKEY"
]
},
"Status": "ACTIVE"
}
]
}
]
I’m trying to filter only master.attributes.key in [‘FIRSTKEY’, ‘SECONDKEY’] and expecting a response as shown below
[
{
"field1": "value1",
"field2": "value2",
"master": [
{
"attributes": {
"key": [
"FIRSTKEY"
]
},
"Status": "ACTIVE"
},
{
"attributes": {
"key": [
"SECONDKEY"
]
},
"Status": "ACTIVE"
}
]
}
]
I tried the following aggregate command
db.collection.aggregate([
{
$match: {
"master.attributes.key": {
$in: [
"FIRSTKEY",
"SECONDKEY"
]
}
}
},
{
$project: {
master: {
$filter: {
input: "$master",
as: "master",
cond: {
$in: [
"$$master.attributes.key",
[
"FIRSTKEY",
"SECONDKEY"
]
]
}
}
},
_id: 0
}
}
])
But I got below response instead of filtered . Appreciate any help here
[
{
"master": []
}
]