Reputation: 375
I am trying to make a group by in Mongo and the problem is that I'm getting no result. I want to do the equivalent to:
select nodo, count(*) as cantidad group by nodo
So, my Mongo query is:
var start = ISODate("2018-08-01T00:00:00.000Z").getTime() / 1000;
var end = ISODate("2018-08-01T23:59:59.000Z").getTime() / 1000;
print("nodo;cantidad");
db.reclamosTecnicos.aggregate([
{ $group : { _id : "$cliente.nodoCrm", cantidad : {$sum : 1} } },
{ $match : { tipoResolucion: {$eq: 0}, fechaCarga: { $gt: start, $lt: end } } }
]).forEach(function(doc){
print(doc._id+";"+doc.cantidad);
})
But I'm getting no result. I thought the problem was the $match part, but if I run this separately I get a result, as you can see in this image:
Can you see what I'm doing wrong?
Upvotes: 1
Views: 1337
Reputation: 46481
Your $match
stage should be the first stage before the $group
.
db.reclamosTecnicos.aggregate([
{ $match : { tipoResolucion: {$eq: 0}, fechaCarga: { $gt: start, $lt: end } } },
{ $group : { _id : "$cliente.nodoCrm", cantidad : {$sum : 1} } }
])
Upvotes: 1
Reputation: 375
As stated by Anthony in a comment, I had to put $match before $group.
Upvotes: 0