我正在努力增加机会的值(value)。
出于某种原因,它不会修改它,我认为它与 $inc
中的行有关。
这是我的代码:
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);
});
结果:
{ ok: 1, nModified: 0, n: 0 }
为什么不修改?
型号:
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,
}
}
],
}
],
编辑:
发现 locations.location.easycrime 不存在。 将代码更新为:
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);
});
但仍然无法工作 { ok: 0, n: 0, nModified: 0 }
编辑 2:
尝试了以下查询:
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);
});
但是 console.log("inserted chance");
和 console.log(result);
不会被执行。 (意味着,它不会运行)
最佳答案
您的代码在 locations.location.easycrime
中看起来是 easycrime
,它存在于模型中的 locations.easycrime
中。我修改了。
另外 locations.easycrime
是一个数组,所以你应该使用 locations.$.easycrime.$.chance
而不是 locations.$.easycrime[t ].机会
.
还注意到,您查询查找 option
但使用 t
更新它。使两者与下面相同。
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);
});
关于javascript - Mongoose 查询不会被修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40577980/