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?
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} } }
])
正如 Anthony 在评论中所说,我不得不将 $match 放在 $group 之前。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.