1

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.

blm
  • 2,379
  • 2
  • 20
  • 24
user3202499
  • 87
  • 2
  • 14

2 Answers2

3

You should call db.loadDatabase(); again at the end of db.update(); to see, that no second row with the same _id: appears instead the specific doc gets directly updated.

Edit: It appears that sometimes when you do db.update() the document that should be updated appears twice instead on the database. It happened to me when I was updating an entry in a list of servers, multiple entries with the modified values were appearing on the database. So to avoid this simply do the following. (I took the same code that was suggested as the answer)

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

             db.loadDatabase();
           }
           );

Doing this prevents this from happening. I tested it multiple times and the issue disappeared.

Anubis Lockward
  • 115
  • 2
  • 11
stefan
  • 41
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 03 '21 at 11:44
1

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);});
deamentiaemundi
  • 5,502
  • 2
  • 12
  • 20
  • 1
    Hi thankyou for the suggestion. I did try this but it still enters a new record instead of updating the existing. – user3202499 Nov 08 '15 at 03:32
  • The single difference is in using the db direct. That might be the problem? – deamentiaemundi Nov 08 '15 at 03:44
  • Hi, thankyou for the suggestion i could get it working without the db direct ...i figured out that i was using the _id to access the row which is generated by nedb to assign an unique key to each row. I stopped using the _id to access the row and it works fine though i don't know the reason why. – user3202499 Nov 08 '15 at 19:45
  • You cannot change `_id`, that's right but you should be able to search for it. It is either something that seemed unrelated on first sight or you found a bug in NEDB. You might think about filing a bug report at .https://github.com/louischatriot/nedb/issues. Please compose code that makes your error easily (e.g. by C&P into `node`) replicable by others when you file the bug. – deamentiaemundi Nov 08 '15 at 20:02