如何返回在最顶部的数组上带有过滤器的子数组的集合?

时间:2019-03-05 18:28:41

标签: node.js mongodb aggregation-framework

我想获取特定序数的所有杂务人员数据

以下是初始数据:

    [
        {
            "_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详细信息< / p>

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

    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"] }
                }
          }
        }}
      ])

1 个答案:

答案 0 :(得分:1)

您可以尝试以下汇总:

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

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