Nedb update and delete method creates a new record instead of updating an existing one

I am using nedb and I am trying to update an existing record by matching its ID and changing the title property. What happens is that a new record is created, and the old one is still there. I tried several combinations and tried searching the site, but the search results are small.

 var Datastore = require('nedb'); var db = { files: new Datastore({ filename: './db/files.db', autoload: true }) }; db.files.update( {_id: id}, {$set: {title: title}}, {}, callback ); 

Even more crazy when performing the deletion, the new entry is added again, but this time the entry has a strange property: {"$$deleted":true,"_id":"WFZaMYRx51UzxBs7"}

This is the code I'm using:

 db.files.remove({_id: id}, callback); 
+4
source share
1 answer

The nedb docs say the following:

localStorage has size restrictions, so it is probably recommended that you install recurring compaction every 2-5 minutes to save space if your client needs lots of updates and deletes. See database compaction for more information on the add-on format used by NeDB only.

Database compaction

Under the hood, NeDB persistence uses a format for adding only, which means that all updates and deletes actually result in added lines at the end of the data file. The reason for this is that disk space is very cheap and add-ons are much faster than rewriting because they are not looking. The database is automatically compacted (i.e., returned back to the format of one line per document) every time your application restarts.

You can manually call the compaction function using yourDatabase.persistence.compactDatafile, which takes no arguments. This queues the compaction of the data file in the executor, which must be performed sequentially after all pending operations.

You can also set automatic compression at regular intervals using yourDatabase.persistence.setAutocompactionInterval (interval), the interval in milliseconds (at least 5 seconds are respected), and the compression automatically stops using yourDatabase.persistence.stopAutocompaction ().

Keep in mind that compaction takes a little time (not too much: 130 ms for 50 thousand records on my slow machine), and no other operation can happen when this happens, so most projects do not actually need to use it.

I have not used this, but it seems to use localStorage and has only append format for update and delete methods.

When examining his source codes in this search in persistence.tests, they wanted to make sure they checked the $$delete key, they also mentioned `If doc contains $$ deleted: true, that means we need to remove it from the data. '

So, in my opinion, you can try to compress db manually or in your question; the second way may be helpful.

+8
source

All Articles