TinyDB

4.8.2 · maintenance · verified Fri Apr 10

TinyDB is a lightweight, document-oriented NoSQL database for Python, optimized for simplicity and happiness. It stores data in human-readable JSON files and is designed for small applications that don't require a full-featured database server. Currently in maintenance mode (v4.8.2), it focuses on bugfixes and community-contributed features, with new releases for minor updates and patches.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a TinyDB database, insert, query, update, and delete documents using `TinyDB` and `Query` objects. It saves data to a local JSON file.

from tinydb import TinyDB, Query
import os

db_file = 'my_db.json'
# Clean up previous db file for fresh run
if os.path.exists(db_file):
    os.remove(db_file)

# Initialize database
db = TinyDB(db_file)

# Insert documents
db.insert({'name': 'Alice', 'age': 30, 'city': 'New York'})
db.insert({'name': 'Bob', 'age': 24, 'city': 'London'})
db.insert({'name': 'Charlie', 'age': 30, 'city': 'Paris'})

# Query documents
User = Query()
alice = db.search(User.name == 'Alice')
print(f"Alice: {alice}")

people_in_30s = db.search(User.age >= 30)
print(f"People aged 30 or more: {people_in_30s}")

# Update documents
db.update({'age': 31}, User.name == 'Alice')
alice_updated = db.search(User.name == 'Alice')
print(f"Alice after update: {alice_updated}")

# Delete documents
db.remove(User.name == 'Bob')
all_users = db.all()
print(f"All users after Bob's removal: {all_users}")

# Clean up after example
os.remove(db_file)

view raw JSON →