2015-11-08 67 views
0

我在nedb中有以下數據。在nedb中更新一行

["UserId":"1446943507761","UserName":"xxx","link":"xxx.html","taskDone":"false","id":14,"_id":"fdaaTWSxloQZdYlT"] 
["UserId":"1446943507761","UserName":"xxx","link":"xxx.html","taskDone":"false","id":1,"_id":"fzh2cedAXxT76GwB"] 
["UserId":"1446943507761","UserName":"xxx","link":"xxx.html","taskDone":"false","id":0,"_id":"k4loE7XR5gioQk54"] 

我想更新id爲0的行,並將taskDone的值設置爲true。我使用以下查詢將值設置爲true

db.taskmap.update({ _id: "k4loE7XR5gioQk54", UserName:"xxx" }, { $set: { taskDone: "true"} }, function (err, numReplaced) { 
    console.log("replaced---->" + numReplaced); 
    }); 

它更新值,但它更新爲新行。它基本上插入一個具有相同值的新行,除了taskdone值爲true。它不會刪除現有數據。因此,在更新後的最終數據表中,我得到了id 0的兩行,除了taskDone之外,所有值都相同。我不確定我是否做錯了什麼。如果有人能告訴我更新值的正確方法,這將會很有幫助。

回答

1

update希望四個參數

var Datastore = require('nedb'); 
var db = new Datastore(); 

db.insert(
[ 
    { 
    "UserId":"1446943507761", 
    "UserName":"xxx", 
    "link":"xxx.html", 
    "taskDone":"false", 
    "id":14, 
    "_id":"fdaaTWSxloQZdYlT" 
    }, 
{ 
    "UserId":"1446943507761", 
    "UserName":"xxx", 
    "link":"xxx.html", 
    "taskDone":"false", 
    "id":1, 
    "_id":"fzh2cedAXxT76GwB" 
}, 
{ 
    "UserId":"1446943507761", 
    "UserName":"xxx", 
    "link":"xxx.html", 
    "taskDone":"false", 
    "id":0, 
    "_id":"k4loE7XR5gioQk54" 
    }], 
    function (err, newDocs) { 
    // empty here 
    } 
); 
db.update(
      { _id: "k4loE7XR5gioQk54", UserName:"xxx" }, 
      { $set: { taskDone: "true"} }, 
      {},// this argument was missing 
      function (err, numReplaced) { 
      console.log("replaced---->" + numReplaced); 
      } 
      ); 
// should give the correct result now 
db.find({}).exec(function (err, docs) {console.log(docs);}); 
+0

喜三江源的建議。我確實嘗試過,但它仍然輸入新記錄而不是更新現有記錄。 – user3202499

+0

唯一的區別在於使用db direct。這可能是問題所在? – deamentiaemundi

+0

嗨,謝謝你的建議,我可以得到它的工作沒有數據庫直接...我想通過使用_id訪問由nedb生成的行爲每行分配一個唯一的密鑰。我停止使用_id訪問該行,但它工作正常,但我不知道原因。 – user3202499