Tinydb 简明教程

TinyDB - The any() Query

对于搜索包含列表的字段,TinyDB 提供了一个名为 any() 的方法。此方法至少匹配数据库中的一个给定值。它根据所提供的查询找到一个完整列表或一个最小值。

For searching the fields containing a list, TinyDB provides a method called any(). This method matches at least one given value from the database. It finds either an entire list or a minimum one value as per the query provided.

Syntax

TinyDB any () 的语法如下 -

The syntax of TinyDB any() is as follows −

db.search(Query().field.any(query|list)

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

  1. If we will provide query as the argument of any() method, it will match all the documents where at least one document in the list field match the given query.

  2. On the other hand, if we will provide list as the argument of any() method, it will match all the documents where at least one document in the list field is present in the given list.

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

让我们看看如何从我们的 student 表中查找 subject 为 TinyDB 或 MySQL 或 SQL 或任意两个或三个组合的字段 −

Let’s see how we can find the fields from our student table where subject is either TinyDB, or MySQL, or SQL or combination of any two or three −

from tinydb import TinyDB, Query
db = TinyDB('student.json')
db.search(Query().subject.any(['TinyDB', 'MySQL', 'oracle']))

上面的查询将获取 "subject" 字段包含以下值的 "TinyDB"、"MySQL" 或 "oracle" 的所有行 −

The above query will fetch all the rows where the "subject" field contains any of the following values: "TinyDB", "MySQL", or "oracle" −

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

Example 2

让我们看看当 any() 方法在给定列表中没有匹配任何内容时如何做出反应 −

Let’s see how the any() method reacts when it doesn’t match anything from the given list −

from tinydb import TinyDB, Query
db = TinyDB('student.json')
db.search(Query().subject.any(['Oracle']))

此查询将返回一个空值,因为没有 "subject" 为 "Oracle" 的行。

This query will return a blank value because there are no rows with its "subject" as "Oracle".

[]

Example 3

请注意,它是区分大小写的。"subject" 字段没有 " Oracle ",但有 " oracle "。尝试以下查询 −

Observe that it is case-sensitive. The "subject" field does not have "Oracle", but it does have "oracle". Try the following query −

from tinydb import TinyDB, Query
db = TinyDB('student.json')
db.search(Query().subject.any(['oracle']))

它将获取以下行 −

It will fetch the following row −

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

由于它是区分大小写的,因此在前面的示例中返回了一个空值,因为没有 "subject" 为 "Oracle" 的行。

As it is case-sensitive, it returned a blank value in the previous example because there are no rows with its "subject" as "Oracle".