TinyDB - 搜索



TinyDB 提供了 **search()** 方法来帮助您搜索文档中的任何数据。结合 **query()** 对象,search() 方法可用于在 JSON 文件中查找数据。我们有多种方法可以使用 TinyDB 数据库上的 search() 方法。

方法 1:使用字段存在性进行 TinyDB search()

我们可以根据字段的存在性从数据库中搜索数据。让我们通过一个例子来理解它。在本例和其他示例中,我们将使用以下 **student** 数据库。

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

示例

基于字段存在性的搜索查询如下所示:

from tinydb import Query
student = Query()
db.search(student.address.exists())

以上查询将从 **student** 文件中检索以下数据:

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

方法 2:使用正则表达式进行 TinyDB search()

我们可以使用正则表达式 (Regex) 从数据库中搜索特定数据。让我们通过几个例子来了解它是如何工作的。

示例 1

与正则表达式匹配的完整项目搜索:

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

此查询将产生以下 **输出**:

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

示例 2

使用正则表达式的区分大小写的搜索:

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

它将产生以下 **输出**:

[{
   'roll_number': 4,
   'st_name': 'lakan',
   'mark': 200,
   'subject': 'MySQL',
   'address': 'mumbai'
}]

示例 3

与正则表达式匹配的项目的任何部分:

from tinydb import Query
student = Query()
db.search(student.st_name.search('r+'))

此查询将产生以下 **输出**:

[{
   'roll_number': 5,
   'st_name': 'karan',
   'mark': 275,
   'subject': 'TinyDB',
   'address': 'benglore'
}]

方法 3:使用子字符串进行 TinyDB search()

在从 TinyDB 数据库搜索特定数据时,我们还可以使用子字符串。让我们通过几个例子来了解它是如何工作的:

示例 1

看看这个查询;它将获取“address”字段为“delhi”的所有行。

from tinydb import Query
student = Query()
db.search(student['address'] == 'delhi')

它将产生以下 **输出**:

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

示例 2

在这个查询中,我们使用了 search() 方法略微不同的语法。

from tinydb import Query
student = Query()
db.search(student.address.search('mumbai'))

它将获取“address”字段为“mumbai”的所有行。

[{
   'roll_number': 4,
   'st_name': 'lakan',
   'mark': 200,
   'subject': 'MySQL',
   'address': 'mumbai'
}]
广告