2016-11-13 28 views
1

私のチャンスの価値を高めることは苦労しています。 なんらかの理由で、それを修正することはありません。私はそれが$incの行と関係があると思います。ここでmongooseクエリが変更されることはありません

は私のコードです:

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, 
        } 
       } 
      ], 
     } 
    ], 

EDIT:

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 }

EDIT 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);は文句を言わないで実行すること。 (意味、実行しません)

+0

を動作するはずですしてみてください、あなたは場所を見つけ、そして場所に、あなたはeasycrimeを見つけます。それは私がほしいと思うものです。 @アルーナ、私は間違いを見ましたが、それを修正するかどうかは分かりません。 – maria

+0

@Aruna、スレッドを更新しました。 – maria

+0

他のいくつかの提案について私の答えを見てください – Aruna

答えて

1

easycrimelocations.location.easycrimeあなたのモデルのlocations.easycrimeに存在します。私はそれを変更しました。

また、locations.easycrimeは配列なので、locations.$.easycrime[t].chanceの代わりにlocations.$.easycrime.$.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); 
}); 
+0

私は試してみると実行されません。 – maria

+0

http://prntscr.com/d6uy3e結果:チャンスを増やす 更新チャンス500。 – maria

+0

質問をあなたが試したもので更新できますか?私はこれのためのサンプルを作成し、まもなく同じことを試みます:-) – Aruna

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); 
}); 
関連する問題