使用$选择mongoose中的子文档

问题描述 投票:0回答:1

这是我的文档结构:

"event": {
    "attendants": {
        "seekers": [
            {
                "$oid": "5bcdabd27e51de001576d289"
            },
            {
                "$oid": "5bc9b39c1a48dd0d7d521924"
            },
        ],
        "employers": [
            {
                "id": {
                    "$oid": "5bcda6477e51de001576d287"
                },
                "boothVisits": []
            },
            {
                "id": {
                    "$oid": "5bd0787e0f64cd001563ddbf"
                },
                "boothVisits": [
                    {
                        "$oid": "5bcda6477e51de001576d287"
                    },
                    {
                        "$oid": "5bd0787e0f64cd001563ddbf"
                    },
                ]
            },
        ]
    },
}

我需要选择当前用户的id在employees表中的事件。但是还有boothVisits那里阻止了简单的in检查。这就是我试图做的事情

Event.find().where('attendants.employers.$.id').equals(req.user._id).catch(e => console.log(e)),

但它没有用。我错过了什么?我假设我以错误的方式使用$

javascript node.js mongodb mongoose
1个回答
1
投票

如果要返回整个文件,那么您不需要使用$

只需使用如下:

const mongoose = require('mongoose');
Event.find({"attendants.employers.id": mongoose.Types.ObjectId(req.user._id)}),

这将检查雇主阵列下的id如果找到则将返回整个事件。

© www.soinside.com 2019 - 2024. All rights reserved.