如何在mongodb中重新分组我的展开项目?

如何在mongodb中重新分组我的展开项目?,mongodb,mongoose,aggregation-framework,Mongodb,Mongoose,Aggregation Framework,我正在构建看板板,我需要将我的整个看板文档作为一个小型文档进行投影 它是一个看板对象,列作为嵌入文档,卡片作为每列的子文档 我有以下数据集: { "_id": { "$oid": "609b2ab4c60588f6b0579259" }, "title": "Entregas", "ownerId": "6026af58714618ac7735

我正在构建看板板,我需要将我的整个看板文档作为一个小型文档进行投影

它是一个看板对象,列作为嵌入文档,卡片作为每列的子文档

我有以下数据集:

{
  "_id": {
    "$oid": "609b2ab4c60588f6b0579259"
  },
  "title": "Entregas",
  "ownerId": "6026af58714618ac77356d2c",
  "columns": [
    {
      "headerText": "Backlog",
      "keyField": "Backlog",
      "cards": []
    },
    {
      "headerText": "Column 1",
      "keyField": "Column 1",
      "cards": [
        {
          "design": "default",
          "rank": 0,
          "labels": [
            {
              "color": "#61bd4f",
              "text": "",
              "active": false
            },
            {
              "color": "#eb5a46",
              "text": "",
              "active": true
            },
            {
              "color": "#f5dd29",
              "text": "",
              "active": false
            },
            {
              "color": "#ff9f1a",
              "text": "",
              "active": true
            },
            {
              "color": "#c377e0",
              "text": "",
              "active": true
            },
            {
              "color": "#0079bf",
              "text": "",
              "active": false
            }
          ],
          "description": "Ah valeyhjs",
          "_id": {
            "$oid": "609d3a63fc07b66ba8168215"
          },
          "title": "test2",
          "parentColumn": "Column 1",
          "creationDate": {
            "$date": "2021-05-13T14:40:35.497Z"
          },
          "updatedAt": {
            "$date": "2021-05-13T14:43:49.287Z"
          }
        },
        {
          "design": "default",
          "rank": 0,
          "labels": [
            {
              "color": "#61bd4f",
              "text": "",
              "active": false
            },
            {
              "color": "#eb5a46",
              "text": "",
              "active": false
            },
            {
              "color": "#f5dd29",
              "text": "",
              "active": false
            },
            {
              "color": "#ff9f1a",
              "text": "",
              "active": false
            },
            {
              "color": "#c377e0",
              "text": "",
              "active": false
            },
            {
              "color": "#0079bf",
              "text": "",
              "active": false
            }
          ],
          "description": "",
          "_id": {
            "$oid": "609e6ee8865e834d14bd1ff5"
          },
          "title": "xd",
          "parentColumn": "Column 1",
          "creationDate": {
            "$date": "2021-05-14T12:36:56.473Z"
          },
          "updatedAt": {
            "$date": "2021-05-14T12:36:56.473Z"
          }
        }
      ]
    },
    {
      "headerText": "Column 2",
      "keyField": "Column 2",
      "cards": [
        {
          "design": "default",
          "rank": 0,
          "labels": [
            {
              "color": "#61bd4f",
              "text": "",
              "active": false
            },
            {
              "color": "#eb5a46",
              "text": "",
              "active": false
            },
            {
              "color": "#f5dd29",
              "text": "",
              "active": false
            },
            {
              "color": "#ff9f1a",
              "text": "",
              "active": false
            },
            {
              "color": "#c377e0",
              "text": "",
              "active": false
            },
            {
              "color": "#0079bf",
              "text": "",
              "active": false
            }
          ],
          "description": "",
          "_id": {
            "$oid": "609d3984fc07b66ba8168214"
          },
          "title": "test",
          "parentColumn": "Column 2",
          "creationDate": {
            "$date": "2021-05-13T14:36:52.899Z"
          },
          "updatedAt": {
            "$date": "2021-05-13T14:36:52.899Z"
          }
        }
      ]
    }
  ],
  "creationDate": {
    "$date": "2021-05-12T01:09:09.000Z"
  },
  "updatedAt": {
    "$date": "2021-05-18T11:52:45.925Z"
  },
  "__v": 52
}
这是我的聚合

    [
    {
        '$match': {
            '_id': ObjectId('609b2ab4c60588f6b0579259')
        }
    }, {
        '$unwind': {
            'path': '$columns'
        }
    }, {
        '$unwind': {
            'path': '$columns.cards', 
            'preserveNullAndEmptyArrays': True
        }
    }, {
        '$project': {
            'title': 1, 
            'ownerId': 1, 
            'columns.headerText': 1, 
            'columns.keyField': 1, 
            'columns.cards.labels': 1, 
            'columns.cards.title': 1, 
            'columns.cards.parentColumn': 1, 
            'columns.cards._id': 1, 
            'columns.cards.attachments': {
                '$size': {
                    '$ifNull': [
                        '$columns.cards.attachments', []
                    ]
                }
            }, 
            'columns.cards.checkLists': {
                '$size': {
                    '$ifNull': [
                        '$columns.cards.checklLists', []
                    ]
                }
            }, 
            'columns.cards.activity': {
                '$size': {
                    '$ifNull': [
                        '$columns.cards.activity', []
                    ]
                }
            }, 
            'creationDate': 1, 
            'updatedAt': 1
        }
    }, {
        '$group': {
            '_id': '$_id', 
            'title': {
                '$first': '$title'
            }, 
            'ownerId': {
                '$first': '$ownerId'
            }, 
            'columns': {
                '$push': '$columns'
            }, 
            'creationDate': {
                '$first': '$creationDate'
            }, 
            'updatedAt': {
                '$first': '$updatedAt'
            }
        }
    }
]
我试图只获取卡子文档中每个数组的大小。 我的问题是我拿错了最终文件的卡片。我需要把他们分组
使用它们的父列。

您可以使用
$map
尝试不使用
$unwind

  • $map
    迭代
    列的循环
  • $map
    迭代
    卡的循环
    数组
  • 添加所需字段,并使用
    $mergeObjects
  • $mergeObjects
    合并
    列和
    卡的当前文档
    更新字段

我注意到我没有将附件、检查表和活动数组放在示例数据集上。。它很有魅力,非常感谢!
db.collection.aggregate([
  { "$match": { "_id": ObjectId("609b2ab4c60588f6b0579259") } },
  {
    $addFields: {
      columns: {
        $map: {
          input: "$columns",
          in: {
            $mergeObjects: [
              "$$this",
              {
                cards: {
                  $map: {
                    input: "$$this.cards",
                    in: {
                      $mergeObjects: [
                        "$$this",
                        {
                          attachments: {
                            $size: { $ifNull: ["$$this.attachments", []] }
                          }
                        },
                        {
                          checkLists: {
                            $size: { $ifNull: ["$$this.checkLists", []] }
                          }
                        },
                        {
                          activity: {
                            $size: { $ifNull: ["$$this.activity", []] }
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      }
    }
  }
])