MongoDB 聚合嵌套$group

问题描述 投票:0回答:1

我正在尝试在单个聚合请求中呈现统计信息。分组和子分组!

const [
    { type: "agency", properties: 4 },
    { type: "host", properties: 5 },
    { type: "agency", properties: 4 },
    { type: "landlord", properties: 5 },
    { type: "agency", properties: 8 },
    { type: "tenant", properties: 2 },
    { type: "host", properties: 1 },
    { type: "host", properties: 1 },
    { type: "agency", properties: 8 },
    { type: "host", properties: 9 },
    { type: "host", properties: 5 },
    { type: "agency", properties: 1 },
    { type: "agency", properties: 2 },
    { type: "tenant", properties: 2 },
    { type: "agency", properties: 1 },
    { type: "tenant", properties: 4 },
    { type: "tenant", properties: 7 }
];

pipeline.push({
    $group: {
        _id: "$type",
        count: { $count: {} },
        "0 property": {
            $sum: {
                "properties": {
                    $eq: 0
                }
            }
        },
        "1 property": {
            $sum: {
                "properties": {
                    $eq: 1
                }
            }
        },
        "2 properties": {
            $sum: {
                "properties": {
                    $eq: 2
                }
            }
        },
        "3 or more properties": {
            $sum: {
                "properties": {
                    $gte: 3
                }
            }
        }
    }
});

想要的结果

[
    {
        type: "tenant",
        count: 4, // THERE ARE 4 TENANTS TOTAL
        "0 properties": 0, // 0 TENANTS WHERE PROPERTIES = 0
        "1 property": 0, // 0 TENANTS WHERE PROPERTIES = 1
        "2 properties": 2, // 2 TENANTS WHERE PROPERTIES = 2
        "3 or more properties": 2 // 2 TENANTS WHERE PROPERTIES >= 3
    },
    {
        type: "landlord",
        count: 1, // THERE IS 1 LANDLORDTOTAL
        "0 properties": 0,
        "1 property": 0,
        "2 properties": 0,
        "3 or more properties": 1
    },
    {
        type: "agency",
        count: 7, // THERE ARE 7 AGENTSTOTAL
        "0 properties": 0,
        "1 property": 2,
        "2 properties": 1,
        "3 or more properties": 4
    },
    {
        type: "host",
        count: 5, // THERE ARE 5 HOSTS TOTAL
        "0 properties": 0,
        "1 property": 1,
        "2 properties": 0,
        "3 or more properties": 3
    }
]

结果将首先按类型分组,对于每个分组类型,我们将按范围进行子分组,然后可以将其呈现为表格/矩阵!

我已经通过一系列单独的请求矩阵完成了此操作,每个请求一个!

上面的这段代码显然不正确,无法运行。有谁知道该怎么做?

丹尼尔

mongodb aggregate
1个回答
0
投票

一种方法是根据匹配的条件将“0 属性”、“1 属性”等字段设置为 0 和 1。然后对类型进行分组并对这些字段的值求和。它有点长/重复,但有效。 (寻找更好的方法,但在那之前。)

db.collection.aggregate([
  {
    $set: {
      "0 properties": { $cond: [{ $eq: ["$properties", 0] }, 1, 0] },
      "1 property": { $cond: [{ $eq: ["$properties", 1] }, 1, 0] },
      "2 properties": { $cond: [{ $eq: ["$properties", 2] }, 1, 0] },
      "3 or more properties": { $cond: [{ $gte: ["$properties", 3] }, 1, 0] }
    }
  },
  {
    $group: {
      _id: "$type",
      count: { $count: {} },
      "0 properties": { $sum: "$0 properties" },
      "1 property": { $sum: "$1 property" },
      "2 properties": { $sum: "$2 properties" },
      "3 or more properties": { $sum: "$3 or more properties" }
    }
  },
  {
    $set: {
      _id: "$$REMOVE",
      type: "$_id"
    }
  }
])

蒙戈游乐场

对于它的价值,我会将这些标签更改为字符串化整数,并在最终投影阶段处理标签。上面的管道让它变得更加明显。

© www.soinside.com 2019 - 2024. All rights reserved.