Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete record or document from TinyDB

How to delete record or document from TinyDB

Example of DB:

{"1" : {"id_key" : "xxx", "params" : {} } },
{"2" : {"id_key" : "yyy", "params" : {} } },

I want to delete "1" if id_key=='xxx'

On TinyDB tutorial code below is suggested. How to complete it to delete record/document ?

db.update(delete('key1'), where('key') == 'value')
like image 306
surge_ Avatar asked Jun 24 '15 09:06

surge_


People also ask

How do you delete on TinyDB?

Updating data TinyDB comes with these operations: delete(key) : delete a key from the document. increment(key) : increment the value of a key.

Is TinyDB fast?

TinyDB. Source. Similar to pickleDB and Shelve, TinyDB is another fast and lightweight key-value store. TinyDB prides itself on being ultra slim, quite fast and wildly simple to use.


1 Answers

To use the example code for your data, type:

db.update(delete('id_key'), where('id_key') == 'xxx')

Please note: TinyDB is a key-value database. using the code above will remove the key 'xxx'. If you type:

db.all()

you will see that the key 'xxx' is deleted. but realize that the row still exists in the database AND if 'params' had any values in it, the values in 'params' would still exist.

A better alternative might be to use TinyDB's remove command, for example:

db.remove(where('id_key') == 'xxx')
like image 63
klitz1967 Avatar answered Sep 21 '22 05:09

klitz1967