NGabioud
NGabioud

Reputation: 375

MongoDB Aggregate shows no result

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:

enter image description here

Can you see what I'm doing wrong?

Upvotes: 1

Views: 1337

Answers (2)

Ashh
Ashh

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

NGabioud
NGabioud

Reputation: 375

As stated by Anthony in a comment, I had to put $match before $group.

Upvotes: 0

Related Questions