【发布时间】:2021-05-18 20:58:28
【问题描述】:
我正在构建一个看板,我需要将我的整个看板文档投影为一个缩小文档。
它是一个看板对象,列作为嵌入文档,卡片作为每列的子文档。
我有这个数据集:
{
"_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'
}
}
}
]
我试图仅获取 Card 子文档中每个数组的大小。 我的麻烦是我的最终文件使用了错误的卡片。我需要将它们归为一组 与它们的父列。
【问题讨论】:
标签: mongodb mongoose aggregation-framework