首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >避免MongoDB聚合中的空卷支撑

避免MongoDB聚合中的空卷支撑
EN

Stack Overflow用户
提问于 2021-11-04 02:21:02
回答 1查看 198关注 0票数 0

我试图通过react显示mongodb聚合结果。我的部分问题是,我没有在后端实现正确的聚合输出。

当前聚合输出,其中包含不必要的空大括号

代码语言:javascript
复制
[{
  "_id":"Fubar",
  "A_set":[{"A":"Y"},{"A":"N"},{},{}],
  "A_count_set":[{"A_count":1},{"A_count":1},{},{}],
  "B_set":[{},{},{"B":"N"},{"B":"Y"}],
  "B_count_set":[{},{},{"B_count":1},{"B_count":1}]},
{
"_id":"Fubar2",
  "A_set":[{"A":"Y"},{"A":"N"},{},{}],
  "A_count_set":[{"A_count":1},{"A_count":1},{},{}],
  "B_set":[{},{},{"B":"N"},{"B":"Y"}],
  "B_count_set":[{},{},{"B_count":1},{"B_count":1}]
}]

我试图实现这个目标聚合输出,它缺少空的大括号

代码语言:javascript
复制
[{
  "_id":"Fubar",
  "A_set":[{"A":"Y"},{"A":"N"}],
  "A_count_set":[{"A_count":1},{"A_count":1}],
  "B_set":[{"B":"N"},{"B":"Y"}],
  "B_count_set":[{"B_count":1},{"B_count":1}]},
{
"_id":"Fubar2",
  "A_set":[{"A":"Y"},{"A":"N"}],
  "A_count_set":[{"A_count":1},{"A_count":1}],
  "B_set":[{"B":"N"},{"B":"Y"}],
  "B_count_set":[{"B_count":1},{"B_count":1}]
}]

流水线操作

代码语言:javascript
复制
{$facet: {
      A_branch: [
        {$group: {
          _id: {
            Q_id: "$A_id",
            A_id: "$A_id"
          },
          A_count: {$sum: 1}
        }}
      ],
      B_branch: [
        {$group: {
          _id: {
            Q_id: "$Q_id",
            B_id: "$B_id"
          },
          B_count: {$sum: 1}
        }}
      ]
    }},
    {$project: {
      combined_group: {$setUnion: ['$A_branch','$B_branch']}
    }},
    {$unwind: '$combined_group'},
    {$lookup:
      {
        from: "Q",
        localField: "combined_group._id.Q_id",
        foreignField: "_id",
        as: "QRef" 
      }
    },
    {$unwind: "$QRef" },
    {$lookup:
      {
        from: "A",
        localField: "combined_group._id.A_id",
        foreignField: "_id",
        as: "ARef" 
      }
    },
    {$unwind: {path:"$ARef", preserveNullAndEmptyArrays: true} },
    {$lookup:
      {
        from: "B",
        localField: "combined_group._id.B_id",
        foreignField: "_id",
        as: "BRef" 
      }
    },
    {$unwind: {path:"$BRef", preserveNullAndEmptyArrays: true} },
    {$group: {
      _id: "$QRef.text",
      A_set: {
        $push: {
          A: "$ARef.value"
        }
      },
      A_count_set: {
        $push: {
          A_count: "$combined_group.A_count"
        }
      },
      B_set: {
        $push: {
          B: "$BRef.value"
        }
      },
      B_count_set: {
        $push: {
          B_count: "$combined_group.B_count"
        }
      }
    }}

聚合输入

代码语言:javascript
复制
{
        "_id" : ObjectId("618..."),
        "Q_id" : ObjectId("618..."),
        "B_id" : ObjectId("618..."),
        "A_id" : ObjectId("618...")
}
{
        "_id" : ObjectId("618..."),
        "Q_id" : ObjectId("618..."),
        "B_id" : ObjectId("618..."),
        "A_id" : ObjectId("618...")
}
{
        "_id" : ObjectId("618..."),
        "Q_id" : ObjectId("618..."),
        "B_id" : ObjectId("618..."),
        "A_id" : ObjectId("618...")
}
{
        "_id" : ObjectId("618..."),
        "Q_id" : ObjectId("618..."),
        "B_id" : ObjectId("618..."),
        "A_id" : ObjectId("618...")
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-04 02:47:33

在管道末端使用$filter

代码语言:javascript
复制
db.collection.aggregate([
  {
    "$set": {
      "A_set": {
        "$filter": {
          "input": "$A_set",
          "as": "x",
          "cond": { "$ne": [ "$$x", {}] }
        }
      }
    }
  },
  {
    "$set": {
      "A_count_set": {
        "$filter": {
          "input": "$A_count_set",
          "as": "x",
          "cond": { "$ne": [ "$$x", {}] }
        }
      }
    }
  },
  {
    "$set": {
      "B_set": {
        "$filter": {
          "input": "$B_set",
          "as": "x",
          "cond": { "$ne": [ "$$x", {}] }
        }
      }
    }
  },
  {
    "$set": {
      "B_count_set": {
        "$filter": {
          "input": "$B_count_set",
          "as": "x",
          "cond": { "$ne": [ "$$x", {}] }
        }
      }
    }
  }
])

蒙古操场

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69833859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档