在猫鼬中更新子文档中数组的最后一个对象

时间:2020-09-15 12:25:10

标签: node.js mongodb mongoose mongodb-query mongoose-schema

我有一个猫鼬模式,其中包含对象relatives的数组。在relatives数组中,我还有另一个对象history数组。我想更新history数组中的最后一个对象。我有ObjectIdrelatives的{​​{1}},但没有history数组的最后一个元素。我该怎么办?

模式

history

我正在尝试做类似的事情-

relatives:[
{
history: [
    {
        purposeOfVisit: {
            type: String,
        },
        date:{
            type:String
        }
     }]
}]

我知道我不能像const user_details = await user.findById(sub); const relatives = await user_details.relatives.id(relativesId); const lengthOfHistory = relatives.history.length; await patient_details.update({ $set: { history[lengthOfHistory-1]: { purposeOfVisit:"xyz" }, }, }); 那样写作。因此,我该怎么做?

0 个答案:

没有答案