"aggregate with unwind" 的结果与 "find with count" 的结果不同?

Result from "aggregate with unwind" is different from the "find with count"?

这是我的一些文档 collections:

{"make":"Lenovo", "model":"Thinkpad T430"},
{"make":"Lenovo", "model":"Thinkpad T430", "problems":["Battery"]},
{"make":"Lenovo", "model":"Thinkpad T430", "problems":["Battery","Brakes"]}

如您所见,有些文档没有问题,有些文档只有 一个问题,有些文档几乎没有问题 在列表中。

我想计算问题列表中有多少评论有特定的 "problem"(如 "Battery")。

我尝试使用以下聚合命令:

{ $match : { model : "Thinkpad T430"} },
{ $unwind : "$problems" },
{ $group: {
        _id: '$problems',
        count: { $sum: 1 }
}}

对于电池问题,计数是 382。我还决定用 find()count():

仔细检查这个结果
db.reviews.find({model:"Thinkpad T430",problems:"Battery"}).count()

结果是 362。

为什么我有这个区别?正确的计算方法是什么?

您可能在集合中有文档,其中 problems 在数组中包含多个 "Battery" 字符串。

当使用$unwind时,它们将各自产生自己的文档,因此后续的$group操作将单独计算它们。