【问题标题】:update an element inside an array of object is not working in Mongoose更新对象数组中的元素在 Mongoose 中不起作用
【发布时间】:2018-06-20 22:19:03
【问题描述】:

我有 mongoose 查询来更新一个文档,我必须在其中检查文档 _id 和对象数组中的另一个 id。我的查询是:

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

我需要找到一个带有_id 的文档,并且只在inv_users 中更新survey_completed

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,
        },
    }]
});

【问题讨论】:

  • 看起来不错;有什么不好的地方?
  • 这就是我想知道的!它没有更新。我想知道为什么。
  • 是不是偶然this issue
  • 我不认识男人!我已经在 ROBO 中尝试过。它不工作!
  • 我只是在几乎相同的问题上悬赏。如果您找到解决方案,请分享

标签: node.js mongodb mongoose


【解决方案1】:

有同样的问题,只是一起取出了 $set 并且它起作用了

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 2019-03-20
    • 2013-03-19
    相关资源
    最近更新 更多