3

我真的很挣扎,所以我希望有人能帮助我理解为什么我会出现这种行为。

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

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

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

4

2 回答 2

3

如果您想真正“更新”和“删除”,NeBD 使用仅附加模式来提高性能,您应该压缩您的数据库,请参阅这个相关问题我相信这个解释让您了解更多关于 Nedb - nedb 方法更新和删除创建一个新条目而不是更新现有条目

于 2017-02-09T03:00:27.030 回答
2

所以事实证明这是在文档中解释的。

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

于 2017-02-03T23:46:25.660 回答