gpt4 book ai didi

javascript - nedb 方法更新和删除创建一个新条目而不是更新现有条目

转载 作者:搜寻专家 更新时间:2023-10-31 22:58:27 25 4
gpt4 key购买 nike

我正在使用 nedb 我正在尝试通过匹配它的 ID 并更改 title 属性来更新现有记录。发生的情况是创建了一条新记录,而旧记录仍然存在。我尝试了几种组合,并尝试用谷歌搜索它,但搜索结果很少。

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

db.files.update(
{_id: id},
{$set: {title: title}},
{},
callback
);

执行删除时更疯狂的是,会再次添加一条新记录,但这次记录有一个奇怪的属性:{"$$deleted":true,"_id":"WFZaMYRx51UzxBs7"}

这是我正在使用的代码:

db.files.remove({_id: id}, callback);

最佳答案

在 nedb 文档中,它说了以下内容:

localStorage has size constraints, so it's probably a good idea to setrecurring compaction every 2-5 minutes to save on space if your clientapp needs a lot of updates and deletes. See database compaction formore details on the append-only format used by NeDB.

Compacting the database

Under the hood, NeDB's persistence uses an append-only format, meaningthat all updates and deletes actually result in lines added at the endof the datafile. The reason for this is that disk space is very cheapand appends are much faster than rewrites since they don't do a seek.The database is automatically compacted (i.e. put back in theone-line-per-document format) everytime your application restarts.

You can manually call the compaction function withyourDatabase.persistence.compactDatafile which takes no argument. Itqueues a compaction of the datafile in the executor, to be executedsequentially after all pending operations.

You can also set automatic compaction at regular intervals withyourDatabase.persistence.setAutocompactionInterval(interval), intervalin milliseconds (a minimum of 5s is enforced), and stop automaticcompaction with yourDatabase.persistence.stopAutocompaction().

Keep in mind that compaction takes a bit of time (not too much: 130msfor 50k records on my slow machine) and no other operation can happenwhen it does, so most projects actually don't need to use it.

我没有使用它,但它似乎使用了 localStorage 并且它具有用于更新和删除方法的仅附加格式。

在调查其源代码时,in that search在 persistence.tests 中,他们希望确保检查 $$delete 键,他们还提到`如果文档包含 $$deleted: true,这意味着我们需要将其从数据中删除``。

因此,在我看来,您可以尝试手动或在您的问题中压缩数据库;第二种方法可能有用。

关于javascript - nedb 方法更新和删除创建一个新条目而不是更新现有条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32038709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com