TinyDB - 搜索


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

方法 1:TinyDB search() 与字段的存在性

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

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

上述查询将从学生文件中检索以下数据-

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

看看这个查询;它将获取“地址”字段为“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'))

它将获取“地址”字段为“孟买”的所有行。

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