Tinydb 简明教程
TinyDB - The exists() Query
TinyDB 提供了一个高级查询,叫做 exists() ,该查询检查 JSON 文件中数据的可用性。 exists() 查询实际测试了来自 JSON 文件的子字段数据的可用性。exists() 查询基于布尔条件工作。如果子字段存在(即布尔 TRUE),它将从 JSON 文件中相应地获取数据,否则它将返回一个空白值。
Syntax
TinyDB exists() 的语法如下 -
db.search(Query().field.exists())
此处, field 表示我们要访问的数据部分。 Query() 是由 JSON 表 student 创建的对象。
我们将使用我们在所有前一章中都曾使用过的相同的 student 数据库。
Example 1
让我们给名为 ' subject ' 的字段使用 TinyDB exists() 查询 -
db.search(student.subject.exists())
此查询将获取到所有行,因为所有行都有 "subject" 字段 -
[
{
"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
现在让我们给 ' address ' 字段使用 exists() 查询 -
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"
}
]