用猫鼬更新嵌套数组

时间:2018-09-08 21:42:17

标签: node.js mongoose

我正在尝试更新嵌套数组,但我的模型似乎无法正常工作

model.Client = mongoose.model('Client', new Schema({
    agencies: [
        new Schema({
            agency: {type: ObjectId, ref: 'Agency'},
            dwellings: [
                new Schema({
                    dwelling: {type: ObjectId, ref: 'Dwelling'},
                }, {_id: false})]
        }, {_id: false})
    ],

}, {collection: 'clients', timestamps: true}));

及其更新代码

model.Client.findOneAndUpdate({_id: dwelling.client.value, 'agencies.agency': dwelling.agency}, {
                $push: {
                    agencies: {
                        dwellings: new ObjectId(dwelling._id),
                    }
                }
            }).exec();

现在我正在被其取代的机构内部找到新的住所

{
    "agency": {
        "$oid": "5b9428b85374382bc051a895"
    },
    "dwellings": [{"$oid": "newoid"}]
},

0 个答案:

没有答案