无法更新mongodb中的数据

swa*_*ram 3 mongoose mongodb node.js express mongodb-query

我需要更新一些我正在使用 mongoose 驱动程序和 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
    }]
});
Run Code Online (Sandbox Code Playgroud)

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

所以在这些我需要通过 idpresentRound scheduleInterviewStatus interviewStatusroundStatus(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"
    }]
Run Code Online (Sandbox Code Playgroud)

更新后:

    presentRound: 2,
    scheduleInterviewStatus: false,
    interviewStatus: "in-process",
    ratings: [{
        round: 1,
        rating: 3,
        feedback: "good communicationskills",
        interviewer: "Vishal",
        roundStatus: "selected"
    }]
Run Code Online (Sandbox Code Playgroud)

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

Error: Fourth argument must be empty when specifying upsert and multi with an object.
Run Code Online (Sandbox Code Playgroud)

询问:

   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})
Run Code Online (Sandbox Code Playgroud)

我不知道我哪里出错了

请帮忙。

chr*_*dam 5

您的update陈述不正确,它的参数放错了位置 - 您将多个$set操作和选项作为不同的参数放入更新方法;它们应该在单独的指定更新参数下。正确的 Node.js语法是:

update(selector, document, options, callback)
Run Code Online (Sandbox Code Playgroud)

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

你在这里做

update(selector, document, selector, document, options, options, callback)
Run Code Online (Sandbox Code Playgroud)

其中 mongo 使用前两个参数更新集合是正确的,它自然会抛出错误

错误:使用对象指定 upsert 和 multi 时,第四个参数必须为空。

因为您指定了太多不正确的参数。

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


为了正确实施,请遵循此更新

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 }
)
Run Code Online (Sandbox Code Playgroud)