You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改MongoDB Charts聚合管道获取no_stage2统计数据

MongoDB聚合管道修改:统计进入stage3的cid路径分类

集合文档示例

{  "_id":"630c7a2e5ecd8b099047df24",  "cid": "10000",  "stage": "stage1"}
{  "_id":"630c7a2e5ecd8b099047df25",  "cid": "10001",  "stage": "stage1"}
{  "_id":"630c7a2e5ecd8b099047df26",  "cid": "10002",  "stage": "stage1"}
{  "_id":"630c7a2e5ecd8b099047re35",  "cid": "10000",  "stage": "stage2"}
{  "_id":"630c7a2e5ecd8b099047re78",  "cid": "10002",  "stage": "stage2"}
{  "_id":"630c7a2e5ecd8b099047x567",  "cid": "10000",  "stage": "stage3"}
{  "_id":"630c7a2e5ecd8b099047xas6",  "cid": "10001",  "stage": "stage3"}
{  "_id":"630c7a2e5ecd8b099047qwe2",  "cid": "10002",  "stage": "stage3"}

需求说明

需要统计进入stage3的cid的两种路径:

  • has_stage2:经历过stage2再进入stage3的cid,数量为2
  • no_stage2:直接从stage1进入stage3(未经历stage2)的cid,数量为1

最终输出格式要求:

label               arc: distinct(cid)
0  has_stage2          2
1  no_stage2           1

原聚合管道

[   {      "$addFields":{         "cid":{            "$convert":{               "input":"$cid",               "to":"string",               "onError":null            }         }      }   },   {      "$match":{         "stage":{            "$in":["stage2", "stage3"]         }      }   },   {      "$addFields":{         "stage":{            "$switch":{               "branches":[                  {                     "case":{                        "$in":[                           {                              "$toString":"$stage"                           },                           [                              {                                 "$literal":"stage2"                              }                           ]                        ]                     },                     "then":"has_stage2"                  },                  {                     "case":{                        "$in":[                           {                              "$toString":"$stage"                           },                           [                              {                                 "$literal":"stage3"                              }                           ]                        ]                     },                     "then":"has_stage3"                  }               ],               "default":"Other values"            }         }      }   },   {      "$group":{         "_id":{            "__alias_0":"$stage"         },         "__alias_1":{            "$addToSet":"$cid"         }      }   },   {      "$project":{         "_id":0,         "__alias_0":"$_id.__alias_0",         "__alias_1":{            "$size":"$__alias_1"         }      }   },   {      "$project":{         "value":"$__alias_1",         "label":"$__alias_0",         "_id":0      }   },   {      "$addFields":{         "__agg_sum":{            "$sum":[               "$value"            ]         }      }   },   {      "$sort":{         "__agg_sum":-1      }   },   {      "$project":{         "__agg_sum":0      }   },   {      "$limit":5000   }]

修改后的聚合管道

[
  {
    "$addFields": {
      "cid": {
        "$convert": {
          "input": "$cid",
          "to": "string",
          "onError": null
        }
      }
    }
  },
  // 按cid分组,收集每个cid经历的所有stage
  {
    "$group": {
      "_id": "$cid",
      "stages": { "$addToSet": "$stage" }
    }
  },
  // 筛选出包含stage3的cid(仅关注最终到达stage3的记录)
  {
    "$match": {
      "stages": "stage3"
    }
  },
  // 判断cid是否经历过stage2,生成对应标签
  {
    "$addFields": {
      "label": {
        "$cond": {
          "if": { "$in": ["stage2", "$stages"] },
          "then": "has_stage2",
          "else": "no_stage2"
        }
      }
    }
  },
  // 按标签分组,统计不同路径的cid数量
  {
    "$group": {
      "_id": "$label",
      "value": { "$sum": 1 }
    }
  },
  // 调整输出格式
  {
    "$project": {
      "_id": 0,
      "label": "$_id",
      "value": 1
    }
  },
  {
    "$sort": { "value": -1 }
  }
]

修改说明

  • 按cid分组收集全量stage:这是核心改动,通过$group将同一个cid的所有阶段记录整合到数组中,完整还原每个cid的路径历程。
  • 聚焦到达stage3的cid:通过$match过滤掉未到达stage3的记录,缩小统计范围到需求目标群体。
  • 生成路径分类标签:用$cond判断cid的阶段数组中是否包含stage2,直接生成has_stage2或no_stage2的分类标签。
  • 统计分类数量:最后按标签分组,用$sum统计每个分类下的cid数量,得到符合需求的统计结果。

内容的提问来源于stack exchange,提问作者TMPS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 12:54:25