I have an array of objects and in the $project pipeline of MongoDB I have to choose the one element whose metadata does not exist. So, for example below are a few documents after my $group pipeline -
{
_id: {
genre: "suspense",
},
price: 10210.6,
data: [
{
subGenre: "Thriller",
flag: true,
},
{
subGenre: "jumpScare",
},
{
subGenre: "horror",
flag: true,
}
]
}
After this, I need to run a $project pipeline where I have to only project that element of the data array where the flag does not exist. My syntax for that is -
db.collection.aggregate([
{
"$project": {
"_id": 0,
"price": 1,
"data": {
"$getField": {
"field": "subGenre",
"input": {
"$first": {
"$filter": { input: "$data", cond: { "$exists": [ "$$this.flag", false ] } }
}
}
}
}
}
}
])
But this is throwing an error -
Invalid $project :: caused by :: Unrecognized expression '$exists'
The output should be -
{
price: 10210.6,
subGenre: "jumpScare"
}