更新nedb中的一行

时间:2015-11-08 02:33:09

标签: javascript node.js 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 个答案:

答案 0 :(得分: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);});