gpt4 book ai didi

node.js - 如何返回子数组的聚合,并在最顶部数组上使用过滤器?

转载 作者:太空宇宙 更新时间:2023-11-04 00:00:23 24 4
gpt4 key购买 nike

我想获取特定序数的所有编排数据

这是初始数据:

    [
{
"_id": "5c7464a26b47a13470411031",
"affiliation": "liss_family",
"year": 2019,
"weekNumber": 9,
"chart": [
{
"chorePerson": [
{
"_id": "5c7464a26b47a13470411054",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Catbox",
"choreID": "5c6f12cf4b19711d1824b16f"
},
{
"_id": "5c7464a26b47a13470411053",
"person": "Joe_2512",
"personID": "5c6ed442c30c29281cd75425",
"chore": "Sweep",
"choreID": "5c6f168bf09a1e0a50164683"
},
{
"_id": "5c7464a26b47a13470411052",
"person": "Tom_2510",
"personID": "5c703e5c094c084af05d8432",
"chore": "Trash",
"choreID": "5c6f16b2f09a1e0a50164686"
},
{
"_id": "5c7464a26b47a13470411051",
"person": "M_2513",
"personID": "5c745b80a3b3cb3a449b9605",
"chore": "Dishes",
"choreID": "5c7063476ee2723a24707b9d"
}
],
"_id": "5c7464a26b47a13470411050",
"ordinal": 0
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a1347041104f",
"person": "Joe_2512",
"personID": "5c6ed442c30c29281cd75425",
"chore": "Catbox",
"choreID": "5c6f12cf4b19711d1824b16f"
},
{
"_id": "5c7464a26b47a1347041104e",
"person": "Tom_2510",
"personID": "5c703e5c094c084af05d8432",
"chore": "Sweep",
"choreID": "5c6f168bf09a1e0a50164683"
},
{
"_id": "5c7464a26b47a1347041104d",
"person": "M_2513",
"personID": "5c745b80a3b3cb3a449b9605",
"chore": "Trash",
"choreID": "5c6f16b2f09a1e0a50164686"
},
{
"_id": "5c7464a26b47a1347041104c",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Dishes",
"choreID": "5c7063476ee2723a24707b9d"
}
],
"_id": "5c7464a26b47a1347041104b",
"ordinal": 1
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a1347041104a",
"person": "Tom_2510",
"personID": "5c703e5c094c084af05d8432",
"chore": "Catbox",
"choreID": "5c6f12cf4b19711d1824b16f"
},
{
"_id": "5c7464a26b47a13470411049",
"person": "M_2513",
"personID": "5c745b80a3b3cb3a449b9605",
"chore": "Sweep",
"choreID": "5c6f168bf09a1e0a50164683"
},
{
"_id": "5c7464a26b47a13470411048",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Trash",
"choreID": "5c6f16b2f09a1e0a50164686"
},
{
"_id": "5c7464a26b47a13470411047",
"person": "Joe_2512",
"personID": "5c6ed442c30c29281cd75425",
"chore": "Dishes",
"choreID": "5c7063476ee2723a24707b9d"
}
],
"_id": "5c7464a26b47a13470411046",
"ordinal": 2
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a13470411045",
"person": "M_2513",
"personID": "5c745b80a3b3cb3a449b9605",
"chore": "Catbox",
"choreID": "5c6f12cf4b19711d1824b16f"
},
{
"_id": "5c7464a26b47a13470411044",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Sweep",
"choreID": "5c6f168bf09a1e0a50164683"
},
{
"_id": "5c7464a26b47a13470411043",
"person": "Joe_2512",
"personID": "5c6ed442c30c29281cd75425",
"chore": "Trash",
"choreID": "5c6f16b2f09a1e0a50164686"
},
{
"_id": "5c7464a26b47a13470411042",
"person": "Tom_2510",
"personID": "5c703e5c094c084af05d8432",
"chore": "Dishes",
"choreID": "5c7063476ee2723a24707b9d"
}
],
"_id": "5c7464a26b47a13470411041",
"ordinal": 3
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a13470411040",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Catbox",
"choreID": "5c6f12cf4b19711d1824b16f"
},
{
"_id": "5c7464a26b47a1347041103f",
"person": "Joe_2512",
"personID": "5c6ed442c30c29281cd75425",
"chore": "Sweep",
"choreID": "5c6f168bf09a1e0a50164683"
},
{
"_id": "5c7464a26b47a1347041103e",
"person": "Tom_2510",
"personID": "5c703e5c094c084af05d8432",
"chore": "Trash",
"choreID": "5c6f16b2f09a1e0a50164686"
},
{
"_id": "5c7464a26b47a1347041103d",
"person": "M_2513",
"personID": "5c745b80a3b3cb3a449b9605",
"chore": "Dishes",
"choreID": "5c7063476ee2723a24707b9d"
}
],
"_id": "5c7464a26b47a1347041103c",
"ordinal": 4
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a1347041103b",
"person": "Joe_2512",
"personID": "5c6ed442c30c29281cd75425",
"chore": "Catbox",
"choreID": "5c6f12cf4b19711d1824b16f"
},
{
"_id": "5c7464a26b47a1347041103a",
"person": "Tom_2510",
"personID": "5c703e5c094c084af05d8432",
"chore": "Sweep",
"choreID": "5c6f168bf09a1e0a50164683"
},
{
"_id": "5c7464a26b47a13470411039",
"person": "M_2513",
"personID": "5c745b80a3b3cb3a449b9605",
"chore": "Trash",
"choreID": "5c6f16b2f09a1e0a50164686"
},
{
"_id": "5c7464a26b47a13470411038",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Dishes",
"choreID": "5c7063476ee2723a24707b9d"
}
],
"_id": "5c7464a26b47a13470411037",
"ordinal": 5
},
{
"chorePerson": [
{
"_id": "5c7464a26b47a13470411036",
"person": "Tom_2510",
"personID": "5c703e5c094c084af05d8432",
"chore": "Catbox",
"choreID": "5c6f12cf4b19711d1824b16f"
},
{
"_id": "5c7464a26b47a13470411035",
"person": "M_2513",
"personID": "5c745b80a3b3cb3a449b9605",
"chore": "Sweep",
"choreID": "5c6f168bf09a1e0a50164683"
},
{
"_id": "5c7464a26b47a13470411034",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Trash",
"choreID": "5c6f16b2f09a1e0a50164686"
},
{
"_id": "5c7464a26b47a13470411033",
"person": "Joe_2512",
"personID": "5c6ed442c30c29281cd75425",
"chore": "Dishes",
"choreID": "5c7063476ee2723a24707b9d"
}
],
"_id": "5c7464a26b47a13470411032",
"ordinal": 6
}
],
"date": "2019-02-25T21:56:50.737Z",
"__v": 0
}
]

我想要做的是返回特定年份、周数和特定序数(例如:2)的所有编排数据

所以最终结果将如下所示:

    [
{
"_id": "5c7464a26b47a13470411031",
"affiliation": "liss_family",
"year": 2019,
"weekNumber": 9,
"chart": [
{
"chorePerson": [
{
"_id": "5c7464a26b47a1347041104a",
"person": "Tom_2510",
"personID": "5c703e5c094c084af05d8432",
"chore": "Catbox",
"choreID": "5c6f12cf4b19711d1824b16f"
},
{
"_id": "5c7464a26b47a13470411049",
"person": "M_2513",
"personID": "5c745b80a3b3cb3a449b9605",
"chore": "Sweep",
"choreID": "5c6f168bf09a1e0a50164683"
},
{
"_id": "5c7464a26b47a13470411048",
"person": "emily",
"personID": "5c6e3c74b9f5ed0016b00577",
"chore": "Trash",
"choreID": "5c6f16b2f09a1e0a50164686"
},
{
"_id": "5c7464a26b47a13470411047",
"person": "Joe_2512",
"personID": "5c6ed442c30c29281cd75425",
"chore": "Dishes",
"choreID": "5c7063476ee2723a24707b9d"
}
],
"_id": "5c7464a26b47a13470411046",
"ordinal": 2
}
],
"date": "2019-02-25T21:56:50.737Z",
"__v": 0
}
]

我想我需要在图表上进行过滤..然后填写chorePerson...但是我在过滤序数时遇到问题,并且我不知道如何在结果中获取choreperson详细信息

这是我到目前为止所拥有的:

    ChoreChart.aggregate([
{ "$match": { "affiliation": affiliation, "year": week.year, "weekNumber": week.number } },
{ "$addFields": {
"chart": {
"$map": {
"input": "$chart",
"as": "cc",
"in": {
"_id": "$$cc._id",
"ordinal": "$$cc.ordinal",
"ordinalString": "$$cc.ordinalString",
}
}
},
"$chart": {
"$filter": {
"input": "$$cc.ordinal",
"as": "ccc",
"cond": { "$eq": ["$$ccc.ordinal", "2"] }
}
}
}}
])

更新

提出新问题而不是在这里附加它

最佳答案

您可以尝试以下聚合:

ChoreChart.aggregate([
{ "$match": { "affiliation": affiliation, "year": week.year, "weekNumber": week.number } },
{
$addFields: {
chart: {
$filter: {
input: "$chart",
cond: { $eq: [ "$$this.ordinal", 2 ] }
}
}
}
}
])

$addFields替换现有字段和 $filter过滤内部数组。此外,由于您的序数是一个数字,您还需要在查询中指定数字而不是字符串。

关于node.js - 如何返回子数组的聚合,并在最顶部数组上使用过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55009324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com