【问题标题】:Unable to update the data in mongodb无法更新mongodb中的数据
【发布时间】:2016-12-16 08:04:45
【问题描述】:

我需要更新一些字段 我正在使用猫鼬驱动程序并表达 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);

所以在这些我需要更新 id presentRound 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"
    }]

更新后:

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

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

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})

我不知道我哪里出错了

请帮忙。

【问题讨论】:

  • multi 和 upsert 不能一起使用 stackoverflow.com/questions/16322724/…
  • 为什么需要几个$set?您能否添加一个示例文档以及更新后的预期内容?
  • 我已经更新了我的问题@Amiram Korach
  • 对不起我的错误。您可以将 multi 和 upsert 组合在一起,但将它们放在一起 {upsert: true, multi: true}。我认为您也需要将集合组合到一个对象。看这里:stackoverflow.com/questions/16322724/…

标签: node.js mongodb express mongoose mongodb-query


【解决方案1】:

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

update(selector, document, options, callback)

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

你在这儿

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

其中 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 }
)

【讨论】:

  • @swathianupuram 不用担心。忘了提一下,由于您正在更新单个文档,因此此处不需要{ multi: true } 选项。
【解决方案2】:

替换 {upsert:true} -> {upsert:true,strict: false}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-10
    • 1970-01-01
    相关资源
    最近更新 更多