简体   繁体   中英

update an element inside an array of object is not working in Mongoose

I've the mongoose query to update a document where I've to check the document _id and another id inside an array of object. my query is:

Survey.findOneAndUpdate({"_id":req.params.id,"inv_users.cmp_user_id":cmp_user_id},
{
 $set:{ "inv_users.$.survey_completed":true}
  }

I need to find a document with the _id and update only survey_completed inside inv_users

const SurveySchema = mongoose.Schema({
    name : {
        type : String,
        require : true,
    },
    company_id: {
        type : Schema.ObjectId,
        require:true
    },
    category : {
        id : Schema.ObjectId,
        name : {
            type : String,
            require : true,
        }
    },
    theme : {
        type : Schema.ObjectId,
    },
    display_type : {
        ui : {
            type : String
        },
        randomization  : {
            type : Boolean,
            default : false,
        },
        skip  : {
            type : Boolean,
            default : false,
        },
        pageno : {
            type : Boolean,
            default : false,
        }
    },
    start_datetime : { 
        type: Date,
    },
    end_datetime : { 
        type: Date,
    },
    logo : { 
        type : String,
    },
    is_header  : {
        type : Boolean,
        default : false,
    },
    is_footer  : {
        type : Boolean,
        default : false,
    },
    header_title : { 
        type: String,
    },
    footer_title : { 
        type: String,
    },
    questions : [{
        question : {
            type : String,
        },
        ans_type : {
            type : String,
        },
        options : [{
            type : String,
        }],
        answers : [{
            answer : {
                type : String,
            },
            cmp_user_id : {
                type : Schema.ObjectId,
            },
            global_user_id : {
                type : Schema.ObjectId,
            },
            date_time : {
                type : Date,
                default:Date.now()
            },
            ip : {
                type : String,
            },
            latitude : {
                type : Number,
            },
            longitude : {
                type : Number,
            }
        }]
    }],

    inv_users : [{
        cmp_user_id : {
            type : Schema.ObjectId,
        },
        email : {
            type : String,
        },
        mail_viewed  : {
            type : Boolean,
            default : false,
        },
        ip : {
            type : String,
        },
        latitude : {
            type : Number,
        },
        longitude : {
            type : Number,
        },
        img_read_code : {
            type : String,
        },
        mail_responsed  : {
            type : Boolean,
            default : false,
        },
        survey_completed  : {
            type : Boolean,
            default : false,
        },
    }]
});

had this same issue just took out $set all together and it worked

Survey.findOneAndUpdate({"_id":req.params.id,"inv_users.cmp_user_id":cmp_user_id},
{
"inv_users.$.survey_completed":true
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM