Reputation: 159
Im struggeling to increase the value of my chance.
For some reason, it wont modify it, i think it has something to do with the line within the $inc
.
Here is my code:
var location: 1,
option = 1,
t = option - 1,
increase = 500;
easyCrime.update({userid: playerid, "locations.location" : location, "locations.location.easycrime.id" : option }, {"$inc" : {"locations.$.location.easycrime[t].chance" : increase}}).then(function (result) {
console.log(result);
});
result:
{ ok: 1, nModified: 0, n: 0 }
why isnt this modified?
model:
userid: {
type: String,
default: '57c1c0f3b6b20c011242bf22'
},
locations: [
{
userid: {
type: String,
default: '57c1c0f3b6b20c011242bf22'
},
location: {
type: Number,
default: 1
},
easycrime: [
{
optioname : {
type: String,
default: 'text'
},
chance: {
type: Number,
default: 200
},
id : {
type: Number,
default: 1,
}
}
],
}
],
EDIT:
found out that locations.location.easycrime dont exist. updated code to:
easyCrime.update({userid: playerid, "locations.location" : location, "locations.easycrime.id" : option }, {"$inc" : {"locations.$.easycrime[0].chance" : increase}}).then(function (result) {
console.log("inserted chance");
console.log(result);
return resolve(result);
});
but still wont work { ok: 0, n: 0, nModified: 0 }
EDIT 2:
tried the following query:
var location: 1,
option = 1,
t = option - 1,
increase = 500;
console.log("updating chance " + increase);
easyCrime.update({userid: playerid, "locations.location" : location, "locations.easycrime.id" : option }, {$inc : {"locations.$.easycrime.$.chance" : increase}}).then(function (result) {
console.log("inserted chance");
console.log(result);
});
but console.log("inserted chance");
and console.log(result);
wont be executed. ( means, it wouldnt run)
Upvotes: 1
Views: 105
Reputation: 631
try this should work
var location: 1,
option = 1,
t = option - 1,
increase = 500;
var updateChance={};
updateChance["locations.$.easycrime."+t+".chance"] = increase;
easyCrime.update({userid: playerid, "locations.location" : location, "locations.easycrime.id" : option }, {$inc : updateChance }).then(function (result) {
console.log("inserted chance");
console.log(result);
});
Upvotes: 1
Reputation: 12022
Your code looks easycrime
at locations.location.easycrime
where it exists at locations.easycrime
in your model. I have modified it.
Also locations.easycrime
is an array, so you should use locations.$.easycrime.$.chance
instead of locations.$.easycrime[t].chance
.
Also noticed, you query looks for option
but updating it with t
. Make the both same as below.
var location: 1,
option = 1,
t = option - 1,
increase = 500;
easyCrime.update({userid: playerid, "locations.location" : location, "locations.easycrime.id" : option }, {$inc : {"locations.$.easycrime.$.chance" : increase}}).then(function (result) {
console.log(result);
});
Upvotes: 1