如何过滤以返回猫鼬模式的子集? [英] How do I filter to return a subset of a mongoose schema?

查看:115
本文介绍了如何过滤以返回猫鼬模式的子集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在mongoDB中查询数组中数组的子集.

My objective is to query mongoDB for a subset of an array within an array.

例如,在整个数据集"(如下)中,我想获取所有PersonID为"5c6e3c74b9f5ed0016b00577"的chorePerson记录

For example, in the 'whole Data Set' (below), I would like to get all of the chorePerson records that have a personID of '5c6e3c74b9f5ed0016b00577'

整个数据集

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

所需结果

所以这是我想要的结果.只有与"5c6e3c74b9f5ed0016b00577"的personID匹配的chorePerson结果:

So here is the desired result I want. Only the chorePerson results that match personID of '5c6e3c74b9f5ed0016b00577' :

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

我看了以下两篇文章,似乎聚合可能是一种方法

I have looked at the following two articles, and it seems like aggregation might be the way to go

https://www.devsbedevin. net/mongodb-find-findone-nested-array-filtering-finally/

如何在猫鼬中使用聚合

背景

这是我的模式:

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

然后我尝试将组更改为以下内容:

Then I tried changing the group to the following:

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

这篇关于如何过滤以返回猫鼬模式的子集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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