TinyDB - Modifying the Data



We have already discussed the update query with the help of which we can modify the values as well as handle the data in our database. But the update query such as db.update(fields, query) allows us to update a document by adding or overwriting its values.

But sometimes, we would like to remove one field or need to increment its value. In such cases, we can pass a function instead of fields. We can use the following operations with the update query −

The Increment Query

The increment query, as its name implies, is used to increment the value of a key in the database. The syntax of increment query is as follows −

from tinydb.operations import increment
db.update(increment('key'))

The Add Query

The add query is used to add value to the value of a key. It works for the strings as well. The syntax of add query is as follows −

from tinydb.operations import add
db.update(add(key, value))

The Set Query

This query is used to set the key to the value of the data. The syntax of set query is as follows −

from tinydb.operations import set
db.update(set(key, value))

The Decrement Query

The decrement query is used to decrement the value of a key. The syntax of decrement query is as follows −

from tinydb.operations import decrement
db.update(decrement(key))

The Subtract Query

The subtarct query is used to subtract value from the value of a key. The syntax of subtract query is as follows −

from tinydb.operations import subtract
db.update(subtract(key, value))

The Delete Query

The delete query is used to delete a key from a document. The syntax of delete query is as follows −

from tinydb.operations import delete
db.update(delete(key))

Let's take a couple of examples to demonstrate how you can use these operations along with the update query. We will use the same student database that we have used in all the previous chapters.

Example 1

Let's see how we can increment the marks of a student in our student table −

from tinydb import TinyDB, Query
db = TinyDB('student.json')
from tinydb.operations import increment
db.update(increment('mark'), Query().mark == 275)

It will produce the following output

[5]

The above output shows that it has updated the record whose document ID is 5.

Example 2

Let's see how we can add 5 marks to the marks of a student in our student table −

from tinydb import TinyDB, Query
db = TinyDB('student.json')
from tinydb.operations import add
db.update(add('mark', 5), Query().mark == 200)

It will produce the following output

[4]

The above output shows that it has updated the record whose document ID is 4.

Example 3

Let's see how we can set the marks to 259 where the marks of a student are 250 in our student table −

from tinydb import TinyDB, Query
db = TinyDB('student.json')
from tinydb.operations import add
db.update(add('mark', 259), Query().mark == 250)

It will produce the following output

[1]

The above output shows that it has updated the record whose document ID is 1.

Example 4

Let's see how we can decrement the marks of a student in our student table −

from tinydb import TinyDB, Query
db = TinyDB('student.json')
from tinydb.operations import decrement
db.update(decrement('mark'), Query().mark == 205)

It will produce the following output

[4]

The above output shows that it has updated the record whose document ID is 4.

Example 5

Let's see how we can subtract 5 marks to the marks of a student in our student table −

from tinydb import TinyDB, Query
db = TinyDB('student.json')
from tinydb.operations import add
db.update(add('mark', 5), Query().mark == 204)

It will produce the following output

[4]

The above output shows that it has updated the record whose document ID is 4.

Example 6

Let's see how we can subtract 5 marks to the marks of a student in our student table −

from tinydb import TinyDB, Query
db = TinyDB('student.json')
from tinydb.operations import delete
db.update(delete('mark'), Query().mark == 209)

It will produce the following output

[4]

The above output shows that it has updated the record whose document ID is 4. It will delete the mark field from the database where the value is 209.

Example 7

Let's see how we can update multiple values in a table with a single query −

from tinydb import TinyDB, Query
db = TinyDB('student.json')
from tinydb import where
db.update_multiple([
   ({'st_name':'Eliana'}, where ('roll_number')==1),
   ({'mark':20}, where ('roll_number') ==2)
])

It will produce the following output

[1,2]

The above output shows that it has updated two records whose document IDs are 1 and 2.

Advertisements