Tinydb 简明教程

TinyDB - The matches() Query

matches() 查询将某个 JSON 文件中的数据与给定条件(正则表达式的形式)匹配,并相应地返回结果。如果条件与文件中的数据不匹配,它将返回一个空白值。

The matches() query matches the data from a JSON file with a given condition (in the form of a regular expression) and returns the results accordingly. It will return a blank value if the condition does not match with the data in the file.

Syntax

TinyDB matches() 的语法如下 -

The syntax of TinyDB matches() is as follows −

db.search(Query().field.matches(regular expression))

此处, 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.

让我们借助几个例子来了解它是如何工作的。我们将使用我们在所有前一章中都曾使用过的相同的 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

让我们看看如何使用 matches() 来进行完整项目搜索。

Let’s see how we can use matches() for full item search.

from tinydb import Query
student = Query()
db.search(student.st_name.matches('[aZ]*'))

此查询会提取所有行 -

This query will fetch all the rows −

[
   {
      "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

让我们看看如何使用 matches() 来进行区分大小写搜索。

Let’s see how we can use matches() for case-sensitive search.

from tinydb import Query
import re
student = Query()
db.search(student.st_name.matches('ram', flags=re.IGNORECASE))

此查询会提取其中学生姓名与字符串 "ram" 匹配的行。注意,我们在匹配字符串时使用了忽略大小写的标记。

This query will fetch the rows where the student name matches the string "ram". Observe that we have used a flag to ignore the case while matching the strings.

[{
   'roll_number': 2,
   'st_name': 'Ram',
   'mark': [250, 280],
   'subject': ['TinyDB', 'MySQL'],
   'address': 'delhi'
}]

Example 3

让我们看看如何使用 matches() 来匹配特定项目。

Let’s see how we can use matches() for a particular item.

student = Query()
db.search(student.address.matches('keral'))

此查询会提取其中地址与字符串 "keral" 匹配的行。

This query will fetch the rows where the address matches the string "keral".

[{'roll_number': 3, 'st_name': 'kevin', 'mark': [180, 200], 'subject':
['oracle', 'sql'], 'address': 'keral'}]

Example 4

让我们看看 matches() 在未找到特定项目时会返回什么 -

Let’s see what matches() would return when it does not find a particular item −

student = Query()
db.search(student.address.matches('Ratlam'))

没有其中 "address" 字段与字符串 "Ratlam" 匹配的行,因此它会返回一个空值 -

There are no rows where the "address" field matches the string "Ratlam", hence it will return a blank value −

[]