Im trying to insert new petitor if not exist, else only updating it, but im getting the following error:
(node:4628) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: Updating the path 'petitors' would create a conflict at 'petitors'
This is my model regarding petitors:
petitors : [
{
userid: {type: String, default: 'x'},
amount: {type: Number, default: 0},
reward: {type: Number, default: 0},
}
],
here is the piece of code that is running, but fails and reproduce the code
var newInsertMent = {userid: user._id, amount: 0};
return Competition_global.findOneAndUpdate(
{"petitors.userid": user._id, _id: globalCompetition._id},
{ $addToSet: {"petitors": newInsertMent}, $inc: {"petitors.$.amount": amount}},
{upsert: true,new: true}
).then(function (petitionUpdate) {
console.log(petitionUpdate);
return petitionUpdate;
});
Q: What am i doing wrong here, and why can it create a conflict?
Im trying to insert new petitor if not exist, else only updating it, but im getting the following error:
(node:4628) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: Updating the path 'petitors' would create a conflict at 'petitors'
This is my model regarding petitors:
petitors : [
{
userid: {type: String, default: 'x'},
amount: {type: Number, default: 0},
reward: {type: Number, default: 0},
}
],
here is the piece of code that is running, but fails and reproduce the code
var newInsertMent = {userid: user._id, amount: 0};
return Competition_global.findOneAndUpdate(
{"petitors.userid": user._id, _id: globalCompetition._id},
{ $addToSet: {"petitors": newInsertMent}, $inc: {"petitors.$.amount": amount}},
{upsert: true,new: true}
).then(function (petitionUpdate) {
console.log(petitionUpdate);
return petitionUpdate;
});
Q: What am i doing wrong here, and why can it create a conflict?
Share Improve this question asked Feb 19, 2018 at 10:20 mariamaria 1595 gold badges24 silver badges62 bronze badges 6-
Try removing
amount
key in thenewInsertMent
object. As the error states, thatamount
field will create a conflict with the subsequent"petitors.$.amount"
update. – chridam Commented Feb 19, 2018 at 10:41 - @chridam good idea. unfortunly its still a conflict. – maria Commented Feb 19, 2018 at 11:03
-
How about using
$set
with the positional operator i.e.{ $set: {"petitors.$.reward": newInsertMent.reward }, $inc: {"petitors.$.amount": amount}},
? – chridam Commented Feb 19, 2018 at 11:06 - @chridam still conflict – maria Commented Feb 19, 2018 at 11:07
- with a rewrite in set, i get: MongoError: The positional operator did not find the match needed from the query.@chridam – maria Commented Feb 19, 2018 at 11:09
2 Answers
Reset to default 1I was trying to acplish the same thing in my recent project and got the same error. I solved my problem using two queries
First, let find the User is present and store the result
let result = Competition_global.findOne(
{"petitors.userid": user._id, _id: globalCompetition._id}
});
By using the result if the user not found then push the new user object in the petitors array
if (result === null) {
let result = await Competition_global.insertOne(
{
$push: {
// your new user data
},
}
);
}
else Update the user amount
else {
let result = await Competition_global.updateOne(
{"petitors.userid": user._id, _id: globalCompetition._id},
{
$inc: {"petitors.$.amount": amount},
}
);
}
});
This is a known issue #7003 of mongoose (and may be mongodb)