Tinydb 简明教程

TinyDB - Modifying the Data

我们已经使用 update 查询进行过讨论,借助该查询,我们可以修改值并处理数据库中的数据。但是 update 查询(如 db.update(fields, query) )允许我们通过添加或覆盖它的值来更新文档。

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

顾名思义,增量查询用于增量数据库中键的值。 syntax 增量查询如下所示 -

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

加法查询用于将值添加到键的值。它也适用于字符串。 syntax 加法查询如下所示 -

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

此查询用于将键设置为数据的值。 syntax 设置查询如下所示 -

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

减量查询用于减量键的值。 syntax 减量查询如下所示 -

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

减法查询用于从键的值中减去值。 syntax 减法查询如下所示 -

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

删除查询用于从文档中删除键。 syntax 删除查询如下所示 -

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))

让我们举几个示例来演示如何将这些操作与 update 查询一起使用。我们将使用我们在所有以前的章节中使用的相同 student 数据库。

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

让我们看看如何在我们的 student 表中 increment 学生的成绩 -

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)

它将生成以下 output

It will produce the following output

[5]

以上输出显示它已经更新了文档 ID 为 5 的记录。

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

Example 2

让我们看看如何在我们的 student 表中 add 学生的成绩 5 分 -

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)

它将生成以下 output

It will produce the following output

[4]

以上输出显示它已经更新了文档 ID 为 4 的记录。

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

Example 3

让我们看看如何在我们的 student 表中将分数 set 为 259 的分数设置为 250 -

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)

它将生成以下 output

It will produce the following output

[1]

以上输出显示它已经更新了文档 ID 为 1 的记录。

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

Example 4

让我们看看如何在我们的 student 表中 decrement 学生分数 -

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)

它将生成以下 output

It will produce the following output

[4]

以上输出显示它已经更新了文档 ID 为 4 的记录。

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

Example 5

让我们看看如何在我们的 student 表中 subtract 学生的成绩 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)

它将生成以下 output

It will produce the following output

[4]

以上输出显示它已经更新了文档 ID 为 4 的记录。

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

Example 6

让我们看看如何向我们学生表中的学生成绩中 subtract 增加 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 delete
db.update(delete('mark'), Query().mark == 209)

它将生成以下 output

It will produce the following output

[4]

以上输出显示,它更新了文档 ID 为 4 的记录。它将从数据库中删除值为 209 的分数字段。

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)
])

它将生成以下 output

It will produce the following output

[1,2]

以上输出显示,它更新了两个文档 ID 为 1 和 2 的记录。

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