我的数据库中有mongo集合:

[
    {
        "_id": "59955bb0cd522a00bcaab08c",
        "lang": "en",
        "__v": 0,
        "message_subsets": [
            {
                "identifier": "errors",
                "_id": "59955bb0cd522a00bcaab08d",
                "messages": [
                    {
                        "identifier": "invalid",
                        "message_text": "$FIELD$ is invalid",
                        "_id": "59955bb0cd522a00bcaab091"
                    },
                    {
                        "identifier": "empty",
                        "message_text": "$FIELD$ is required",
                        "_id": "59955bb0cd522a00bcaab090"
                    }
                ]
            }
        ]
    },
    {
        "_id": "59955bb0cd522a00bcaab08c",
        "lang": "ru",
    ...
]


它是由以下猫鼬模式构造的:

const MessageSchema = new mongoose.Schema({
    identifier: {
        type: String,
        unique: true,
        required: true,
        trim: true
    },
    message_text: {
        type: String,
        required: true,
        trim: true
    }
});

const MessageSubsetSchema = new mongoose.Schema({
    identifier: {
        type: String,
        unique: true,
        required: true,
        trim: true
    },
    messages: [MessageSchema]
});

const LangMessageSetSchema = new mongoose.Schema({
    lang: {
        type: String,
        unique: true,
        required: true,
        trim: true
    },
    message_subsets: [MessageSubsetSchema]
});


我想从具有messagesmessage_subsets数组项中检索所有"identifier": "errors",如果其父项具有"lang": "en"
因为我对猫鼬和蒙哥有点陌生,所以我坚持了这一点!!!您能帮我正确查询数据吗?谢谢。

最佳答案

这应该工作:

Collection.find({'messages.message_subsets.identifier':'errors', 'lang': 'en'}, {'messages': 1})

09-18 11:57