如何在最顶部的数组上返回带有过滤器的子数组的聚合? [英] How do I return an aggregate of sub arrays with a filter on the top most array?

查看:8
本文介绍了如何在最顶部的数组上返回带有过滤器的子数组的聚合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)的所有 choreperson 数据

What I would like to do is return all the choreperson data for a particular year, week number and specific ordinal (eg : 2)

所以最终的结果应该是这样的:

So the final result would look like this:

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

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

这是我目前所拥有的:

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

更新

提出新问题而不是在这里解决问题

asking new question instead of tacking it on here

推荐答案

你可以试试下面的聚合:

You can try below aggregation:

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

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

$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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆