TinyDB – Document ID


TinyDB – Document ID



”;


TinyDB uses document ID, represented by doc_id, to access as well as modify the value of documents in a database. Here we will see how we can use this document_id for various operations.

Display Data using Document ID

We can use doc_id in get() method to display the data from a database. Its syntax is as follows −


db.get(doc_id = value)

Check for a Document in a Database

We can use doc_id in contains() method to check if a document is present in a database or not. Its syntax is given below −


db.contains(doc_id = value)

Update All Documents

We can use doc_id in update() method to update all the documents in a database with the given document IDs. Here is its syntax


db.update({key : value}, doc_ids = […])

Remove a Document

We can use doc_id in remove() method to remove a specific document or all the documents in a database with the given document IDs. Its syntax is given below −


db.remove(doc_ids = […])

Let”s take a few examples to demonstrate how you can use doc_id in TinyDB with these methods. We will use the same student database that we have used in all the previous chapters.

Example 1

Let”s see how we can use doc_id to get the data of a specific document from a database −


from tinydb import TinyDB
db = TinyDB(''student.json'')
db.get(doc_id = 5)

It will fetch the data from the document with the doc_id “5”.


{
   ''roll_number'': 5,
   ''st_name'': ''karan'',
   ''mark'': 275,
   ''subject'': ''oracle'',
   ''address'': ''benglore''
}

Example 2

Let”s see how we can use doc_id to check if the database contains a document with a specific ID −


from tinydb import TinyDB
db = TinyDB(''student.json'')
db.contains(doc_id = 15)

Based on the availability of the document, it will return either True or False. In this case, our database does not have a document with the doc_id “15”. Hence, it returns False.


False

Example 3

Let”s see how we can use doc_id to update the documents of our database −


from tinydb import TinyDB
db = TinyDB(''student.json'')
db.update({''mark'':''280''}, doc_ids = [4])

Here, we updated the “marks” field of the document with the doc_id “4”. To check the updated data, use the following query −


print(db.get(doc_id=4))

It will display the updated data of the document with the doc_id “4” −


{
   ''roll_number'': 4,
   ''st_name'': ''lakan'',
   ''mark'': ''280'',
   ''subject'': ''MySQL'',
   ''address'': ''mumbai''
}

Example 4

Let”s see how we can use doc_id to remove specific documents from our database −


from tinydb import TinyDB
db = TinyDB(''student.json'')
db.remove(doc_ids = [3,4])

Here, we removed two documents with doc_ids “3” and “4”. To verify, use the following get() queries −


db.get(doc_id=3)
db.get(doc_id=4)

It will show the following output −


None
None

It means that we have successfully removed the documents with doc_ids “3” and “4”.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *