Tinydb 简明教程
TinyDB - Searching
TinyDB 提供了 search() 方法来帮助你从文档中检索任何数据。配合使用 query() 对象,search() 方法可用于在 JSON 文件中查找数据。有好多种方法可以在 TinyDB 数据库上使用 search() 方法。
TinyDB provides the search() method to help you search any data from a document. Along with the query() object, the search() method can be used to find the data in a JSON file. We have various ways in which we can use the search() method on a TinyDB database.
Method 1: TinyDB search() with Existence of a Field
我们可以根据字段的存在情况从数据库中检索数据。我们通过一个例子来理解这一点。对于此示例以及其他示例,我们将使用以下 student 数据库。
We can search the data from a database based on the existence of a field. Let’s understand it with an example. For this and other examples, we will be using the following student 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"
}
]
Example
基于字段的存在情况进行的搜索查询如下所示 −
The search query based on the existence of a field is as follows −
from tinydb import Query
student = Query()
db.search(student.address.exists())
上述查询会从 student 文件中检索以下数据 −
The above query will retrieve the following data from the student file −
[
{
"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"
}
]
Method 2: TinyDB search() with Regular Expression
我们可以使用正则表达式 (Regex) 从数据库中检索特定的数据。我们通过几个示例来理解它是如何工作的。
We can search for a particular data from a database using regular expression (Regex). Let’s understand how it works with a couple of examples.
Example 1
与正则表达式配对的完整项目搜索 −
Full item search matching the Regular Expression −
from tinydb import Query
student = Query()
db.search(student.st_name.matches('[aZ]*'))
此查询会生成以下 output −
This query will produce the following output −
[
{
"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"
}
]
Example-2
区分大小写的正则表达式搜索 −
Case-sensitive search with Regular Expression −
from tinydb import Query
import re
student = Query()
db.search(student.st_name.matches('lakan', flags=re.IGNORECASE))
它会生成以下 output −
It wil produce the following output −
[{
'roll_number': 4,
'st_name': 'lakan',
'mark': 200,
'subject': 'MySQL',
'address': 'mumbai'
}]
Example-3
与正则表达式配对的项目部分 −
Any part of the item matching with Regular Expression −
from tinydb import Query
student = Query()
db.search(student.st_name.search('r+'))
此查询会生成以下 output −
This query will produce the following output −
[{
'roll_number': 5,
'st_name': 'karan',
'mark': 275,
'subject': 'TinyDB',
'address': 'benglore'
}]
Method 3: TinyDB search() using a Substring
在从 TinyDB 数据库中检索特定数据时,我们还可以使用子字符串。我们通过几个示例来理解它是如何工作的 −
We can also use a substring while searching for a particular data from a TinyDB database. Let’s understand how it works with a couple of examples −
Example-1
看一下此查询;它将获取 "address" 字段为 "delhi" 的所有行。
Take a look at this query; it will fetch the all the rows where the "address" field is "delhi".
from tinydb import Query
student = Query()
db.search(student['address'] == 'delhi')
它将生成以下 output −
It will produce the following output −
[
{
"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"
}
]
Example-2
在此查询中,我们对 search() 方法使用了稍微不同的语法。
In this query, we have used a slightly different syntax for the search() method.
from tinydb import Query
student = Query()
db.search(student.address.search('mumbai'))
它将获取 "address" 字段为 "mumbai" 的所有行。
It will fetch all the rows where the "address" field is "mumbai".
[{
'roll_number': 4,
'st_name': 'lakan',
'mark': 200,
'subject': 'MySQL',
'address': 'mumbai'
}]