How to update a array value in Mongoose

前端 未结 3 516
既然无缘
既然无缘 2020-12-09 05:12

I want to update a array value but i am not sure about the proper method to do it ,so for i tried following method but didnt worked for me.

My model, The childre

3条回答
  •  没有蜡笔的小新
    2020-12-09 05:37

    can follow this

    if childrens contains string values then model can be like:

    childrens: [{
        type : String
    }]
    

    if childrens contains ObjectId values of another collection _id and want populate then model can be like:

    childrens: [{
        type : mongoose.Schema.Types.ObjectId,
        ref: 'refModelName'
    }]
    

    no need to use $set just use $push to insert value in childrens array. so query can be like:

    Employeehierarchy.update(
       { _id: employeeparent._id},
       {"$push": { "childrens": employee._id } }
     ).exec(function (err, managerparent) {
        //
     });
    

提交回复
热议问题