如何返回在最顶部的数组上带有过滤器的子数组的集合? [英] How do I return an aggregate of sub arrays with a filter on the top most array?
问题描述
我想获取特定序数的所有杂项数据
I would like to get all the choreperson data for a specific ordinal
以下是初始数据:
[
{
"_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)的所有编剧数据
What I would like to do is return all the choreperson data for a particular year, week number and specific ordinal (eg : 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
}
]
我在想ed来过滤图表。然后填写chorePerson ...但我在过滤序数时遇到问题,我不知道如何在结果中获取choreperson详细信息
I am thinking that I need to filter on the chart.. then fill in the chorePerson... but I am having an issue with filtering the ordinal and I don't know how to get the choreperson details in the result
这是我到目前为止的内容:
Here is what I have so far:
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"] }
}
}
}}
])
UPDATE
UPDATE
提出新问题,而不是在此处提出问题
asking new question instead of tacking it on here
推荐答案
您可以尝试以下汇总:
ChoreChart.aggregate([
{ "$match": { "affiliation": affiliation, "year": week.year, "weekNumber": week.number } },
{
$addFields: {
chart: {
$filter: {
input: "$chart",
cond: { $eq: [ "$$this.ordinal", 2 ] }
}
}
}
}
])
$ addFields 替换现有字段,并 $ filter 过滤内部数组。另外,由于普通
是数字,因此您还需要在查询中指定数字而不是字符串。
$addFields replaces existing field and $filter filters inner array. Additionally since your ordinal
is a number you also need to specify number not string in your query.
这篇关于如何返回在最顶部的数组上带有过滤器的子数组的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!