【问题标题】:MongoDb aggregate use $sortByCount inside a $groupMongoDb 聚合在 $group 中使用 $sortByCount
【发布时间】:2021-06-11 19:11:18
【问题描述】:

我正在尝试使用 mongoDb 聚合数据以收集每个用户的数据。我有以下查询:

DB.collection.aggregate([
    { "$facet": {
        "instructions": [
                { $match: { company: ref } },
                { $group: {
                    _id : '$user._id',
                    firstName: { $first: "$user.firstName"},
                    lastName: { $last: "$user.lastName"},
                    total: { $sum: 1 },
                    completed: { $sum: { 
                        "$cond": [
                            { "$eq": [ "$completed.value", true ] }, 
                            1, 0
                        ]}
                    },
                    mostPopularProduct: {
                        $sortByCount: "$productType"  
                    },
                    mostPopularType: {
                        $sortByCount: "$instructionType"  
                    }
                }},
            ]
        }},
        { "$project": {
            "perClient": { "instructions": "$instructions"  }
        }}
    }}
])

我正在尝试从与$match 匹配的所有文档中的字符串字段中获取我的集合中的mostPopularProduct(和mostPopularInstruction)。基本上是所有文档中出现最多的产品。组内还有一些计算。

目前,我收到以下错误:"unknown group operator '$sortByCount'"。现在我知道这是因为它在组内被使用,它不能。但我不确定如何达到预期的效果。我尝试使用$sum,但没有达到我想要的效果。不知道是不是我用错了。

有人可以指点我正确的方向吗?

编辑

好的,所以我可能对我想要什么有点含糊。让我澄清一下。

我之前用过$sortByCount,是这样的:

"mostPopularProduct": [
    { $match: { company: ref } },
    { $sortByCount: "$productType"}
],

我得到的结果是:

"mostPopularProduct": [
    {
        "_id": "car",
        "count": 14
    }, {
        "_id": "house",
        "count": 2
    }
]

现在,我想做完全相同的事情,但我希望它在我的组内,所以基本上我想得到以下结果:

"perClient": {
    "instructions": [
        {
            "_id": "5fd3db7427e9bc610e4daeaa",
            "firstName": "firstName",
            "lastName": "lastName",
            "total": 8,
            "completed": 1,
            // NOW HERE ARE THE FIELDS I WANT
            "mostPopularProduct": [
                {
                    "_id": "car",
                    "count": 3
                }
            ]
        }, // all other clients follow
    ]
}

这是我试图在 $group 中使用 $sortByCount 实现的所需结果。

这里是一个例子:https://mongoplayground.net/p/XolUjfDCpxn

【问题讨论】:

  • 如何达到预期的结果 =>很难预测你想要的结果,如果你在你的问题中添加一些示例文档和预期的结果会很有帮助。
  • 谢谢@turivishal,我加了一个例子

标签: javascript mongodb aggregation


【解决方案1】:

示例文件:

[
  { userId: 1, productType: "car" },
  { userId: 1, productType: "car" },
  { userId: 1, productType: "bus" },
  { userId: 1, productType: "bus" },
  { userId: 2, productType: "bus" },
  { userId: 2, productType: "bus" }
]
  • $group by userIdproductType 并计算总和
  • $group by only userId 并准备 productType 的数组并计数
db.collection.aggregate([
  {
    "$facet": {
      "instructions": [
        {
          $group: {
            _id: {
              userId: "$userId",
              productType: "$productType"
            },
            productTypeCount: { $sum: 1 }
          }
        },
        {
          $group: {
            _id: "$_id.userId",
            mostPopularProduct: {
              $push: {
                _id: "$_id.productType",
                count: "$productTypeCount"
              }
            }
          }
        }
      ]
    }
  }
])

Playground

【讨论】:

  • 不会将新字段与先前或现有字段分开吗?
  • 我没有得到你的确切信息,只是发布你的实际文件。
  • 我添加了一个例子。
  • 你能确认一下吗mongoplayground.net/p/MKm5IWg1-yS
  • 谢谢@turivishal,看起来很完美。产品类型在组内,这让我很困惑。
猜你喜欢
  • 2019-01-21
  • 2019-01-31
  • 2023-03-07
  • 2022-01-20
  • 2019-03-27
  • 2016-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多