在猫鼬中通过其ID查找子元素

时间:2018-10-16 15:16:03

标签: javascript node.js mongodb mongoose

所以这有点混乱。这是我的数据库结构:

{
    "_id": {
        "$oid": "5bb21dea2f00236e68edac00"
    },
    "attendants": {
        "employers": [
            {
                "id": {
                    "$oid": "5b6d8af56c61351d6921b29d"
                },
                "boothVisits": []
            }
        ]
    },
    "postings": [],
    "isFinished": false,
    "eventTitle": "Test2",
    "location": "Toronto",
    "startDate": "2018-09-01",
    "endDate": "2018-12-01",
    "startTime": "12:15",
    "endTime": "04:15",
    "createdBy": {
        "$oid": "5b4751df76ebd145d6c1dc2a"
    },
    "__v": 0
}

我想将当前的用户ID推送到attendants.employers数组中的“ boothVisits”数组中。我正在尝试做这样的事情:

const path = `attendants.employers[${req.params._id}].boothVisits`;
Event.findByIdAndUpdate(eventId, {$push: {[path]: req.user._id}}, {$upsert: true},).catch(e => console.log(e)).then(r => console.log('done', r.attendants.employers));

但是意识到这没有任何意义。如何查询正确的id并推送到boothVisits

0 个答案:

没有答案