Aggregate function isn't showing results with a count greater than 1
You should not add 'count': { '$gt': 1 }
in the first $match
stage.
As the count
field is only populated after the $group
stage.
So, you need add another $match
stage after $group
stage for filtering document with the count value is greater than 1.
db.collection.aggregate([
{
"$match": {
"is_song": 1,
"is_soundtrack": 0
}
},
{
"$group": {
"_id": {
"name": "$name",
"artist_id": "$artist_id"
},
"count": {
"$sum": 1
}
}
},
{
$match: {
"count": {
"$gt": 1
}
}
},
{
"$sort": {
"count": -1
}
}
])
Sample Mongo Playground