Tinydb 简明教程
TinyDB - The all() Query
TinyDB 提供了一个名为 all() 的方法,它可以根据提供的查询查找整个值列表。让我们举一个例子,了解它是如何工作的。
TinyDB provides a method called all() that finds an entire list of values as per the query provided. Let’s take an example and find out how it works.
Syntax
TinyDB all() 的语法如下 −
The syntax of TinyDB all() is as follows −
db.search(Query().field.all(query|list)
此处, field 表示我们要访问的数据部分。 Query() 是对我们名为 student 的 JSON 表创建的对象。
Here, field represents the part of data that we want to access. Query() is the object created of our JSON table named student.
-
If we will provide a query as the argument of all() method, it will match all the documents where all documents in the list field match the given query.
-
On the other hand, if we will provide a list as the argument of all() method, it will match all the documents where all documents in the list field are present in the given list.
让我们借助几个例子来了解它是如何工作的。我们将使用我们在所有前一章中都曾使用过的相同的 student 数据库。
Let’s understand how it works with the help of a couple of examples. We will use the same student database that we have used in all the previous chapters.
Example 1
让我们看看如何从我们的 student 表中查找 subject 同时为 TinyDB 和 MySQL 的字段 −
Let’s see how we can find the fields from our student table where the subjects are both TinyDB, and MySQL −
from tinydb import TinyDB, Query
db = TinyDB('student.json')
db.search(Query().subject.all(['TinyDB', 'MySQL']))
此查询将获取以下行 −
This query will fetch the following row −
[{
'roll_number': 2,
'st_name': 'Ram',
'mark': [250, 280],
'subject': ['TinyDB', 'MySQL'],
'address': 'delhi'
}]
Example 2
让我们看看如何使用 all() 获取我们数据库中的全部数据 −
Let’s see how we can use all() to get the entire data from our database −
from tinydb import TinyDB, Query
db = TinyDB('student.json')
db.all()
它将获取链接数据库中的所有行 −
It will fetch all the rows from the linked database −
[
{
"roll_number":1,
"st_name":"elen",
"mark":250,
"subject":"TinyDB",
"address":"delhi"
},
{
"roll_number":2,
"st_name":"Ram",
"mark":[
250,
280
],
"subject":[
"TinyDB",
"MySQL"
],
"address":"delhi"
},
{
"roll_number":3,
"st_name":"kevin",
"mark":[
180,
200
],
"subject":[
"oracle",
"sql"
],
"address":"keral"
},
{
"roll_number":4,
"st_name":"lakan",
"mark":200,
"subject":"MySQL",
"address":"mumbai"
},
{
"roll_number":5,
"st_name":"karan",
"mark":275,
"subject":"TinyDB",
"address":"benglore"
}
]