4

I'm really struggling with this so I'm hoping someone can help me to understand why I'm getting this behaviour.

I have a file with the following records in it

{"Id":"","documentPath":"mission.pdf","dName":"BIOLOGY TEST 1110","dCourseNumber":"1110","dSubject":"BIOL","dTerm":"201690","dTitle":"BIOLOGY TEST 1110","student":"491502","timestamp":1486152313972,"assessed":true,"errorCmt":"","uploadDate":"2/3/2017 14:5","_id":"QwKHQ8sRQdivRJDp"}
{"Id":"","documentPath":"university.pdf","dName":"BIOLOGY TEST 1111","dCourseNumber":"1111","dSubject":"BIOL","dTerm":"201690","dTitle":"BIOLOGY TEST 1111","student":"491502","timestamp":1486151521825,"assessed":false,"errorCmt":"","uploadDate":"2/3/2017 13:52","_id":"mh0pdGERB6fZU4s3"}

in my testdb.js file I have:

var Datastore = require('nedb');
var db = new Datastore({filename: './db.db', autoload: true});

db.update({ _id: "QwKHQ8sRQdivRJDp"}, { $set: { assessed: true} }, {}, function (err, numReplaced) {
    console.log("replaced---->" + numReplaced);
});

db.find({}).exec(function (err, docs) {console.log(docs);});

The find I perform at the end shows that I've updated a single record and prints out the records and they look as I expect them to. If I open up db.db I now see this:

{"Id":"","documentPath":"mission.pdf","dName":"BIOLOGY TEST 1110","dCourseNumber":"1110","dSubject":"BIOL","dTerm":"201690","dTitle":"BIOLOGY TEST 1110","student":"491502","timestamp":1486152313972,"assessed":true,"errorCmt":"","uploadDate":"2/3/2017 14:5","_id":"QwKHQ8sRQdivRJDp"}
{"Id":"","documentPath":"university.pdf","dName":"BIOLOGY TEST 1111","dCourseNumber":"1111","dSubject":"BIOL","dTerm":"201690","dTitle":"BIOLOGY TEST 1111","student":"491502","timestamp":1486151521825,"assessed":false,"errorCmt":"","uploadDate":"2/3/2017 13:52","_id":"mh0pdGERB6fZU4s3"}
{"Id":"","documentPath":"mission.pdf","dName":"BIOLOGY TEST 1110","dCourseNumber":"1110","dSubject":"BIOL","dTerm":"201690","dTitle":"BIOLOGY TEST 1110","student":"491502","timestamp":1486152313972,"assessed":true,"errorCmt":"","uploadDate":"2/3/2017 14:5","_id":"QwKHQ8sRQdivRJDp"}

Any idea why my record is duplicating with the new value and not just getting updated?

Lee
  • 61
  • 1
  • 4

2 Answers2

4

NeBD use append-only patter for performance if you want to really "update" and "delete" you should compact your database, see this related question I belive that this explenation let you know more about the Nedb - nedb method update and delete creates a new entry instead updating existing one

Community
  • 1
  • 1
Paweł Liszka
  • 330
  • 1
  • 2
  • 17
2

So it turns out this is explained in the documentation.

Under the hood, NeDB's persistence uses an append-only format, meaning that all updates and deletes actually result in lines added at the end of the datafile, for performance reasons.

Lee
  • 61
  • 1
  • 4