javascript arrayarrayfilter中的猫鼬冲突变量

时间:2019-07-18 13:13:26

标签: javascript node.js mongodb express mongoose

我有一个这样的模式

var userSchema = mongoose.Schema({
    contact             :[{//1st level
        phone           : String,
        address2            : String,
        city                : String,
        state               : String,
        dept_id         :{
            type                    : Schema.Types.ObjectId,
            ref                     : 'dept'
        }
    }],
    employment_detail   :[{
        employment_date     : Date,
        employment_type     : String,
        dept_id         :{
            type                    : Schema.Types.ObjectId,
            ref                     : 'dept'
        },
    }],
    positions_held      :[{
        post                : String,
        appointment_date    : String,
        dept_id         :{
            type                    : Schema.Types.ObjectId,
            ref                     : 'dept'
        },
    }]
})

当我想使用arrayFilter和dept_id更新联系人数组并同时更新时 Employment_detail使用dept_id显然不允许我更新这些数组,因为positionsheld 最后一个数组也有dept_id,我无法删除dept_id,因为有时我需要一个数组  对象项。这是一个缩短的脚本

UserModel.updateOne({
        _id:userid
    }, 
    {$set:{'contact.$[first].state':'California',
          'employment_detail.$[second].employment_date':Date.now}},
    {arrayFilters:[{'first.dept_id': thisidthedeptid},
                   {'second.dept_id': thisidthedeptid},]} .......

该怎么办,我该怎么办。

0 个答案:

没有答案