Search code examples
javascriptnode.jsnedb

Update a row in nedb


I have the following data in 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"]

I am trying to update row with id 0 and set the value of taskDone to true. I use the following query to set the value to true

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

It updates the value but it updates as a new row. It basically inserts a new row with same values except for the taskdone value as true. It does not delete the existing data. Hence in the final data table after update i get tow rows for id 0 with all values same except for the taskDone. I am not sure if i am doing anything wrong. It will be helpful if anybody can tell me a correct way of updating the value.


Solution

  • update wants four arguments

    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);});