Javascript 在mongoose中使用$

Javascript 在mongoose中使用$,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,这是我的文档结构: "event": { "attendants": { "seekers": [ { "$oid": "5bcdabd27e51de001576d289" }, { "$oid": "5bc9b39c1a48dd0d7d521924" }, ], "employer

这是我的文档结构:

"event": {
    "attendants": {
        "seekers": [
            {
                "$oid": "5bcdabd27e51de001576d289"
            },
            {
                "$oid": "5bc9b39c1a48dd0d7d521924"
            },
        ],
        "employers": [
            {
                "id": {
                    "$oid": "5bcda6477e51de001576d287"
                },
                "boothVisits": []
            },
            {
                "id": {
                    "$oid": "5bd0787e0f64cd001563ddbf"
                },
                "boothVisits": [
                    {
                        "$oid": "5bcda6477e51de001576d287"
                    },
                    {
                        "$oid": "5bd0787e0f64cd001563ddbf"
                    },
                ]
            },
        ]
    },
}
我需要选择当前用户id位于雇主表中的事件。但是也有boothVisits阻止了简单的检查。这就是我试图做的

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

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

如果要返回整个文档,则不需要使用$

使用方法如下:

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

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

那么您希望返回整个文档还是要返回匹配的特定雇主?雇主id位于Attendars.employers arrayThanks中的整个事件!我想得太多了,哈哈。我编辑了答案,使之与问题匹配+为那些不知道ObjectId来自哪里的人添加了猫鼬