Mongodb 蒙戈。如何统计相关文件的数量?

Mongodb 蒙戈。如何统计相关文件的数量?,mongodb,mongoose,aggregation-framework,Mongodb,Mongoose,Aggregation Framework,我使用nodejs和mongodb。我有两个收藏 我想得到所有批次,每个批次都有Ave的数量。一个请求就可以完成吗?我做了这样一个聚合: Ave.aggregate( { $group: { _id: '$lot', total: { $sum: 1 } } }, function (err, avesinlot) { console.log(avesinlot); }); 但我需要在所有批次之间迭代,并为它们分配num ave。大量的时间花在大型收

我使用nodejs和mongodb。我有两个收藏



我想得到所有批次,每个批次都有Ave的数量。一个请求就可以完成吗?我做了这样一个聚合:

Ave.aggregate(
    { $group:
        { _id: '$lot', total: { $sum: 1 } }
    },
    function (err, avesinlot) { console.log(avesinlot); });

但我需要在所有批次之间迭代,并为它们分配num ave。大量的时间花在大型收藏上。我认为这是一个更好的方法。有人能帮忙吗?谢谢。

这在解释您要做的事情时不是很清楚。听起来,您最好在“Lot”模型中存储一个“Ave”对象数组,然后计算其数量。最好只维护一个保持计数的字段,因为这将比聚合更快。谢谢你的回答Neil,但这里是真实代码的litle litle部分,在real app中,Ave更大,每个批次的Ave都非常多(>10000)。所以,我认为,把AVE单独收藏是个好主意。现在我有超过100万只AVE,有1000多只。每个Ave涉及一个地块。我想得到所有的地段,看看每个地段有多少鸟类。
Ave.aggregate(
    { $group:
        { _id: '$lot', total: { $sum: 1 } }
    },
    function (err, avesinlot) { console.log(avesinlot); });