mongoose查询不会被修改

时间:2016-11-13 19:35:15

标签: javascript mongodb mongoose

我努力增加机会的价值。 出于某种原因,它不会修改它,我认为它与$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);不会被执行。 (意思是,它不会运行)

2 个答案:

答案 0 :(得分:1)

您的代码在easycrime处的locations.location.easycrime处显示模型中locations.easycrime处的代码。我修改了它。

同样locations.easycrime是一个数组,因此您应该使用locations.$.easycrime.$.chance而不是locations.$.easycrime[t].chance

另请注意,您查询查找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);
});

答案 1 :(得分:1)

尝试这应该工作

 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);
});