I am trying to find the sum of all stories in my book collections that have a certain batch ID.
When I run the following pipeline,
db.getCollection("book").aggregate(
{
'$match' : {
active: true,
stories:{
$filter: {
input: "$stories",
as: "story",
cond: { $eq: [ "$$story.batch", "JUL-2018" ] }
}
}
}
},
{
'$project' : {
count:{$size: '$stories'}
}
}
,
{
'$group' : {
_id: "1",
story_count: {$sum: '$count'}
}
}
)
I get:
Unable to execute the selected commands
Mongo Server error (MongoCommandException): Command failed with error 2: 'unknown operator: $filter' on server 127.0.0.1:27017.
The full response is:
{
"ok" : 0.0,
"errmsg" : "unknown operator: $filter",
"code" : NumberInt(2),
"codeName" : "BadValue"
}
What am I doing wrong?