English 中文(简体)
TinyDB - The All() Query
  • 时间:2024-12-27

TinyDB - The all() Query


Previous Page Next Page  

TinyDB provides a method called all() that finds an entire pst of values as per the query provided. Let s take an example and find out how it works.

Syntax

The syntax of TinyDB all() is as follows −


db.search(Query().field.all(query|pst)

Here, field represents the part of data that we want to access. Query() is the object created of our JSON table named student.

    If we will provide a query as the argument of all() method, it will match all the documents where all documents in the pst field match the given query.

    On the other hand, if we will provide a pst as the argument of all() method, it will match all the documents where all documents in the pst field are present in the given pst.

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

Let s see how we can find the fields from our student table where the subjects are both TinyDB, and MySQL −


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

This query will fetch the following row −


[{
    roll_number : 2,
    st_name :  Ram ,
    mark : [250, 280],
    subject : [ TinyDB ,  MySQL ],
    address :  delhi 
}]

Example 2

Let s see how we can use all() to get the entire data from our database −


from tinydb import TinyDB, Query
db = TinyDB( student.json )
db.all()

It will fetch all the rows from the pnked database −


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