I need to create an aggregation that looks like this:
Game.aggregate([{
$match: {
"_id": {
"$in": result.games
}
}
},
{
$unwind: "$gamers",
},
{
$group: {
_id: {
$month: "$gamers.playedAt",
},
count: {
$sum: 1
}
}
}
])
However, if the playedAt
attribute is missing in the gamers
array, the query throws an error:
MongoError: can't convert from BSON type missing to Date
Is there a way to set a default value (such as 0) for playedAt
when it is not defined?