2015-11-08 82 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