Tinydb 简明教程
TinyDB - Logical Negate
逻辑否定作为一种反逻辑门起作用。它将匹配与给定查询不匹配的文档。简而言之,它将显示给定命令的相反含义。
Logical Negate works as an inverse logical gate. It will match the documents that don’t match the given query. In simple words, it will display the opposite meaning of the given command.
Syntax
TinyDB Logical Negate 的语法如下 −
The syntax of TinyDB Logical Negate is as follows −
db.search(~(Query().field)
这里, 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. It will fetch the data that represents the opposite meaning of the given command.
让我们举几个例子,看看它是如何工作的。我们将使用我们在所有先前章节中使用的 student 数据库。
Let’s take a couple of examples and see how it works. We will use the same student database that we have used in all the previous chapters.
Example 1
让我们看看如何从我们的 student 表中查找学生姓名不为 ' elen ' 的字段 −
Let’s see how we can find the fields from our student table where the student name is not 'elen' −
from tinydb import TinyDB, Query
db = TinyDB('student.json')
db.search(~(Query().st_name == 'elen'))
上面的查询将获取所有学生姓名不为“elen”的行。
The above query will fetch all the rows where the student name is not "elen".
[
{
"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
让我们看看如何使用逻辑否定避免特定地址 −
Let’s see how we can avoid a particular address using logical negate −
from tinydb import TinyDB, Query
db = TinyDB('student.json')
db.search(~(student.address.one_of(['keral', 'delhi'])))
此查询将获取“地址”字段不包含“keral”或“德里”的所有行。
This query will fetch all the rows where the "address" field does not have either "keral" or "delhi".
[
{
"roll_number":4,
"st_name":"lakan",
"mark":200,
"subject":"MySQL",
"address":"mumbai"
},
{
"roll_number":5,
"st_name":"karan",
"mark":275,
"subject":"TinyDB",
"address":"benglore"
}
]