无法更新mongodb中的数据 [英] Unable to update the data in mongodb

查看:268
本文介绍了无法更新mongodb中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更新一些字段 我正在使用猫鼬驱动程序并表达js

I need to update some fields i am using mongoose driver and express js

模式:

 var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var ProfilesSchema = new Schema({

    presentRound: {
        type: Number,
        default: 1
    },

    scheduleInterviewStatus: {
        type: Boolean,
        default: false
    },

    interviewStatus: String,

    ratings: [{
        round: Number,
        rating: Number,
        feedback: String,
        interviewer: String,
        roundStatus: String
    }]
});

module.exports = mongoose.model('Profiles',ProfilesSchema);

module.exports = mongoose.model('Profiles', ProfilesSchema);

因此在这些情况下,我需要通过id presentRound scheduleInterviewStatus interviewStatusroundStatus(in ratings array by matching round number)

so in these i need to update by id presentRound scheduleInterviewStatus interviewStatus and roundStatus(in ratings array by matching round number)

更新前:

    presentRound: 1,
    scheduleInterviewStatus: true,
    interviewStatus: "on-hold",
    ratings: [{
        round: 1,
        rating: 3,
        feedback: "good communication skills",
        interviewer: "Vishal",
        roundStatus: "second opinion"
    }]

更新后:

    presentRound: 2,
    scheduleInterviewStatus: false,
    interviewStatus: "in-process",
    ratings: [{
        round: 1,
        rating: 3,
        feedback: "good communicationskills",
        interviewer: "Vishal",
        roundStatus: "selected"
    }]

我尝试先在robomongo中运行查询,但出现错误

i have tried to run the query in robomongo first but getting error

Error: Fourth argument must be empty when specifying upsert and multi with an object.

查询:

   db.getCollection('profiles').update({
        "_id": ObjectId("57a9aa24e93864e02d91283c")
    }, {
        $set: {
            "presentRound": 2,
            "interviewStatus":"in process",
            "scheduleInterviewStatus": false          

        }
    },{
        "ratings.$.round": 1
    },{
        $set: {

            "ratings.roundStatus":"selected"


        }
    },
   { upsert: true },{multi:true})

我不知道我要去哪里哪里

I have no idea where i am going wrong

请帮助.

推荐答案

您的 语法为:

Your update statement is incorrect, it has misplaced arguments - you are putting multiple $set operations and options as different parameters to the update method; they should be under separate designated update parameters. The correct Node.js syntax is:

update(selector, document, options, callback)

其中,selector是一个对象,它是更新操作的选择器/查询,document也是一个对象,它是更新文档,最后是一个options对象,默认情况下为null,并且具有可选更新设置.

where selector is an object which is the selector/query for the update operation, document is also an object which is the update document and finally an optionsobject which by default is null and has the optional update settings.

您在这里

update(selector, document, selector, document, options, options, callback)

其中mongo使用正确的前两个参数更新集合,并且自然会引发错误

In which mongo is updating the collection using the first two parameters as correct and it naturally throws the error

错误:指定upsert和multi时,第四个参数必须为空 与一个对象.

Error: Fourth argument must be empty when specifying upsert and multi with an object.

因为您指定了太多错误的参数.

because you have too many incorrect parameters specified.

此外,您对位置运算符的使用不正确.它应该是要更新的文档的一部分,而不是查询中的

Also, you have incorrect usage of the positional operator. It should be part of the document to be updated, not in the query.

要正确实施,请遵循此更新

For the correct implementation, follow this update

db.getCollection('profiles').update(
    /* selector  */
    {
        "_id": ObjectId("57a9aa24e93864e02d91283c"),
        "ratings.round": 1
    }, 
    /* update document */
    {
        "$set": {
            "presentRound": 2,
            "interviewStatus": "in process",
            "scheduleInterviewStatus": false,
            "ratings.$.roundStatus": "selected"    
        }
    },
    /* optional settings */
    { upsert: true, multi: true }
)

这篇关于无法更新mongodb中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆