Reputation: 529
I'm trying to count the occurences from each id_atendentes on the last 7 days/week.
I have the following query:
db.atendimentos.aggregate([
{'$group' :
{'_id' :
{'id_atendente':'$id_atendente', 'date':
{ '$gte': new Date((new Date().getTime() - (7 * 24 * 60 * 60 * 1000))) }
}},
'sum': {'$sum': 1} }
])
I thought that it would work, but it didn't.
I'm aware of the $week operator but I don't think that it does what I want to do.
I've got the following error: A pipeline stage specification object must contain exactly one field.
I guess that it may be something with my 'date': { '$gte' }... part.
Hope to get some help, thanks!
Upvotes: 2
Views: 1859
Reputation: 899
So if I understand right, you would like to get last week documents, and then group them by the id_atendente, and count the amount each atendente occurred during the last week.
If that is the case, you first need to filter out documents from the last week with a $match
stage, and then follow it with a $group
stage to group by the atendente id.
I think the following code will do the job:
db.atendimentos.aggregate([
{
'$match': {
'date': {'$gte': new Date((new Date().getTime() - (7 * 24 * 60 * 60 * 1000)))}
},
},
{
'$group':
{
'_id': "$id_atendente",
'sum': {'$sum': 1}
},
}
])
Upvotes: 2