Nedb更新正在添加新记录,而不是更新现有记录

时间:2017-02-03 20:49:12

标签: node.js nedb

我真的很挣扎,所以我希望有人可以帮助我理解为什么我会这样做。

我有一个包含以下记录的文件

{"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"}

在我的testdb.js文件中我有:

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

我在最后执行的查找显示我已更新单个记录并打印出记录,它们看起来像我期望的那样。如果我打开db.db,我现在看到:

{"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"}

知道为什么我的记录与新值重复而不只是更新?

3 个答案:

答案 0 :(得分:1)

事实证明,这在文档中有解释。

在幕后,NeDB的持久性使用仅附加格式,这意味着出于性能原因,所有更新和删除实际上都会导致在数据文件末尾添加行。

答案 1 :(得分:1)

如果你想真正“更新”和“删除”你应该压缩你的数据库,NeBD使用仅附加模式来表现性能,看到这个相关的问题我相信这个表达让你了解更多关于Nedb的信息 - {{3} }

答案 2 :(得分:-1)

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

试试这个