Mongodb更新路径会产生冲突

问题描述 投票:6回答:1

如果不存在,我会尝试插入新竞争对手,否则只会对其进行更新,但会出现以下错误:

(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:我在这里做错了,为什么会造成冲突?

javascript node.js mongodb mongoose
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.