Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nedb updates are adding new records instead of updating existing one

Tags:

node.js

nedb

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?

like image 691
Lee Avatar asked Aug 31 '25 06:08

Lee


1 Answers

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

like image 143
Paweł Liszka Avatar answered Sep 02 '25 20:09

Paweł Liszka