nodejs mongodb findAndModify异常:E11000重复键错误索引

时间:2014-06-03 14:08:51

标签: node.js mongodb

monogodb数据库:

 { "title" : "A vs B ha",
      "status" : 1,
      "items" : [ 
        { "name" : "0",
          "num" : 0,
          "men" : [ 
            { "user" : "id1",
              "score" : 300 }, 
            { "user" : "id2",
              "score" : 1300 }, 
            { "user" : "id3",
              "score" : 400 } ] }, 
        { "name" : "1",
          "num" : 0,
          "men" : [ 
            { "user" : "id11",
              "score" : 3300 }, 
            { "user" : "id23",
              "score" : 13400 }, 
            { "user" : "id3",
              "score" : 4000 } ] }, 
        { "name" : "2",
          "num" : 0,
          "men" : [] } ],
      "_id" : ObjectId( "538db59fa332f765c0f2c210" ) }

此Node.js代码不起作用:

collection.findAndModify(
{"_id": idSearch
,"items.name":sBetItem,"items.men.user":sUser}
,[]
, {
  $set:{
      "items.men.user":sUser}
  ,$inc: { "items.men.score": sScore }
}
, {new:true, upsert:true}

,function(err, docUser) {
  if (err){
      console.warn(err.message);  // returns error if no matching object found
  }else{
      console.dir(docUser);
  }

});

我想搜索一个玩家的分数,如果它不存在,请插入新的; 如果已经存在,请增加他的分数。 但它有错误:异常:E11000重复键错误索引

0 个答案:

没有答案