【发布时间】: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 interviewStatus 和 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 中运行查询,但出现错误
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