如果不存在,我会尝试插入新竞争对手,否则只会对其进行更新,但会出现以下错误:
(node:4628) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: Updating the path 'competitors' would create a conflict at 'competitors'
这是我关于竞争对手的模型:
competitors : [
{
userid: {type: String, default: 'x'},
amount: {type: Number, default: 0},
reward: {type: Number, default: 0},
}
],
这里是正在运行的代码,但是失败并再现了代码
var newInsertMent = {userid: user._id, amount: 0};
return Competition_global.findOneAndUpdate(
{"competitors.userid": user._id, _id: globalCompetition._id},
{ $addToSet: {"competitors": newInsertMent}, $inc: {"competitors.$.amount": amount}},
{upsert: true,new: true}
).then(function (competitionUpdate) {
console.log(competitionUpdate);
return competitionUpdate;
});
Q:我在这里做错了,为什么会造成冲突?