我的目标是查询MongoDB中数组的子集。
例如,在“整个数据集”(下面)中,我想获取所有具有“5C6E3C74B9F5ED0016B00577”人格的chorePerson记录
整个数据集

[
    {
        "_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
    }
]

预期结果
所以这是我想要的结果。只有chorePerson结果与“5C6E3C74B9F5ED0016B00577”的personID匹配:
[
    {
        "_id": "5c7464a26b47a13470411031",
        "affiliation": "liss_family",
        "year": 2019,
        "weekNumber": 9,
        "chart": [
            {
                "chorePerson": [
                    {
                        "_id": "5c7464a26b47a13470411054",
                        "person": "emily",
                        "personID": "5c6e3c74b9f5ed0016b00577",
                        "chore": "Catbox",
                        "choreID": "5c6f12cf4b19711d1824b16f"
                    }
                ],
                "_id": "5c7464a26b47a13470411050",
                "ordinal": 0
            },
            {
                "chorePerson": [
                    {
                        "_id": "5c7464a26b47a1347041104c",
                        "person": "emily",
                        "personID": "5c6e3c74b9f5ed0016b00577",
                        "chore": "Dishes",
                        "choreID": "5c7063476ee2723a24707b9d"
                    }
                ],
                "_id": "5c7464a26b47a1347041104b",
                "ordinal": 1
            },
            {
                "chorePerson": [
                    {
                        "_id": "5c7464a26b47a13470411048",
                        "person": "emily",
                        "personID": "5c6e3c74b9f5ed0016b00577",
                        "chore": "Trash",
                        "choreID": "5c6f16b2f09a1e0a50164686"
                    }
                ],
                "_id": "5c7464a26b47a13470411046",
                "ordinal": 2
            },
            {
                "chorePerson": [
                    {
                        "_id": "5c7464a26b47a13470411044",
                        "person": "emily",
                        "personID": "5c6e3c74b9f5ed0016b00577",
                        "chore": "Sweep",
                        "choreID": "5c6f168bf09a1e0a50164683"
                    }
                ],
                "_id": "5c7464a26b47a13470411041",
                "ordinal": 3
            },
            {
                "chorePerson": [
                    {
                        "_id": "5c7464a26b47a13470411040",
                        "person": "emily",
                        "personID": "5c6e3c74b9f5ed0016b00577",
                        "chore": "Catbox",
                        "choreID": "5c6f12cf4b19711d1824b16f"
                    }
                ],
                "_id": "5c7464a26b47a1347041103c",
                "ordinal": 4
            },
            {
                "chorePerson": [
                    {
                        "_id": "5c7464a26b47a13470411038",
                        "person": "emily",
                        "personID": "5c6e3c74b9f5ed0016b00577",
                        "chore": "Dishes",
                        "choreID": "5c7063476ee2723a24707b9d"
                    }
                ],
                "_id": "5c7464a26b47a13470411037",
                "ordinal": 5
            },
            {
                "chorePerson": [
                    {
                        "_id": "5c7464a26b47a13470411034",
                        "person": "emily",
                        "personID": "5c6e3c74b9f5ed0016b00577",
                        "chore": "Trash",
                        "choreID": "5c6f16b2f09a1e0a50164686"
                    }
                ],
                "_id": "5c7464a26b47a13470411032",
                "ordinal": 6
            }
        ],
        "date": "2019-02-25T21:56:50.737Z",
        "__v": 0
    }
]

我看了下面的两篇文章,似乎聚合是解决问题的方法
https://www.devsbedevin.com/mongodb-find-findone-with-nested-array-filtering-finally/
How to use Aggregate in mongoose
背景
这是我的方案:
    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;

    const chorePersonSchema = new mongoose.Schema({
        person: {type: String, requried: true},
        personID: {type: String, required: true},
        chore: {type: String, required: true},
        choreID: {type: String, required: true},
    });

    const chartSchema = new mongoose.Schema({
        ordinal: {type: Number, required: true},

        chorePerson : [{ type:chorePersonSchema }]
    });


    // create the schema
    const ChoreChartSchema = new Schema({

        affiliation: {type: String, required: true},
        weekNumber: {type: Number, required: true},
        year: {type: Number, required: true},

        chart:[{type: chartSchema}],

        date: {type: Date, default: Date.now},
    })

    module.exports = ChoreChart = mongoose.model('cm_chorechart', ChoreChartSchema)

试过这个
这就是我尝试过的:
ChoreChart.aggregate([
{ $match : { affiliation: 'liss_family' } }
,{ $group: { "_id": { personID: '5c6e3c74b9f5ed0016b00577' } } }
]
)
.then(stuff =>{
    return res.status(200).json(stuff);
})
.catch(err => {
    return res.status(401).json(err);
})

以上返回:
  [
      {
          "_id": {
              "personID": "5c6e3c74b9f5ed0016b00577"
          }
      }
  ]

然后我尝试将组更改为以下内容:
,{ $group: { "_id": { "chorePerson.personID": '5c6e3c74b9f5ed0016b00577' } } }

结果是:
 "errmsg": "FieldPath field names may not contain '.'.",

最佳答案

可以使用聚合

ChoreChart.aggregate([
  { "$match": { "affiliation": "liss_family" } },
  { "$addFields": {
    "chart": {
      "$map": {
        "input": "$chart",
        "as": "cc",
        "in": {
          "_id": "$$cc._id",
          "ordinal": "$$cc.ordinal",
          "chorePerson": {
            "$filter": {
              "input": "$$cc.chorePerson",
              "as": "dd",
              "cond": { "$eq": ["$$dd.personID", "5c6e3c74b9f5ed0016b00577"] }
            }
          }
        }
      }
    }
  }}
])

07-24 16:40