Skip to main content

PoloDB for Python

Fast, typed Python bindings for PoloDB, an embedded document database with a MongoDB-like API. The database runs in-process and stores its data locally—there is no server to install or manage.

Version 0.2 uses PoloDB Core 5.3, PyO3 0.29, and CPython's stable ABI. Published wheels support CPython 3.10 and newer on Linux, macOS, and Windows.

Installation

uv add polodb-python

The distribution name is polodb-python; uv add polodb refers to a different, obsolete package.

Quick start

from polodb import PoloDB

with PoloDB("app.db") as db:
    books = db["books"]
    inserted = books.insert_one(
        {"title": "The Three-Body Problem", "author": "Liu Cixin", "year": 2008}
    )

    book = books.find_one({"_id": inserted.inserted_id})
    print(book)

    recent = (
        books.find(
            {"year": {"$gte": 2000}},
        )
        .sort({"year": -1})
        .limit(10)
    )

Collections can also be accessed as attributes (db.books), though item access is preferable when a name is dynamic or collides with a database attribute.

Database configuration

PoloDBConfig exposes PoloDB Core's storage settings while preserving its defaults:

from polodb import PoloDB, PoloDBConfig

config = PoloDBConfig(
    init_block_count=32,
    journal_full_size=2_000,
    lsm_page_size=8_192,
    lsm_block_size=8 * 1024 * 1024,
    sync_log_count=500,
)

with PoloDB("app.db", config=config) as db:
    print(db.config)

The same configuration is retained if a PoloDB context is closed and later reopened.

Collection API

Insert and query

result = books.insert_many(
    [
        {"title": "1984", "author": "George Orwell", "year": 1949},
        {"title": "Animal Farm", "author": "George Orwell", "year": 1945},
    ]
)
print(result.inserted_ids)

book = books.find_one({"title": "1984"})
all_orwell = books.find({"author": "George Orwell"}, sort={"year": 1})
for book in books.find_iter({"year": {"$lt": 1950}}):
    print(book)

find() returns a lazy cursor, so documents are decoded as they are consumed rather than loaded into memory at once. Cursors support chainable skip(), limit(), and sort() methods; the equivalent keyword arguments on find() remain available. An omitted filter means an empty filter.

Update and delete

updated = books.update_one(
    {"title": "1984"},
    {"$set": {"in_print": True}},
)
print(updated.matched_count, updated.modified_count)

books.update_many(
    {"author": "Octavia E. Butler"},
    {"$set": {"featured": True}},
    upsert=False,
)

deleted = books.delete_many({"in_print": False})
print(deleted.deleted_count)

Aggregation

authors = books.aggregate(
    [
        {"$match": {"year": {"$gte": 2000}}},
        {"$sort": {"year": -1}},
        {"$limit": 10},
    ]
)

Indexes

index_name = books.create_index({"title": 1}, unique=True)
books.drop_index(index_name)

Counts and drops

print(len(books))
print(books.count_documents())
books.drop()

# Equivalent database-level operation:
db.drop_collection("books")

Transactions

Transactions commit when their context exits normally and roll back when an exception escapes:

with db.transaction() as transaction:
    accounts = transaction["accounts"]
    accounts.update_one({"name": "Ada"}, {"$inc": {"balance": -100}})
    accounts.update_one({"name": "Grace"}, {"$inc": {"balance": 100}})

Manual commit() and rollback() are also available.

BSON values

The binding round-trips the common BSON-compatible Python values:

  • None, bool, int, float, str
  • nested dictionaries, lists, and tuples
  • bytes and bytearray
  • timezone-aware or naive datetime.datetime values (stored with millisecond precision and returned in UTC)
  • compiled regular expressions
  • polodb.ObjectId

Generated _id values are returned as ObjectId instances, so they can be passed directly into later filters:

from polodb import ObjectId

identifier = ObjectId()  # new value
same_identifier = ObjectId(identifier.hex)
assert identifier == same_identifier

Results and errors

Write operations return typed, immutable result objects:

  • InsertOneResult.inserted_id
  • InsertManyResult.inserted_ids
  • UpdateResult.matched_count and .modified_count
  • DeleteResult.deleted_count

They also implement Mapping, preserving dictionary-style reads such as result["modified_count"]. Database-operation failures raise PoloDBError; invalid Python values raise standard TypeError or ValueError exceptions.

Migrating from 0.1

Most CRUD code continues to work. Notable improvements and changes in 0.2 are:

  • generated IDs are ObjectId values instead of lossy strings; use str(id) or id.hex when text is required;
  • write results are typed mapping objects rather than plain dictionaries;
  • find() returns a lazy, chainable cursor instead of an optional list; call .to_list() when a list is needed;
  • len(collection) and count_documents() are preferred; collection.len() remains as a compatibility alias;
  • context managers now close databases and correctly commit or roll back transactions;
  • unsupported BSON values raise an exception instead of silently becoming None or panicking the interpreter.

Development

uv sync
uv run maturin develop
uv run pytest
uv run ruff check .
uv run mypy polodb
uv run ty check
cargo fmt --check
cargo clippy --all-targets --all-features -- -D warnings

Releases are built from vX.Y.Z tags. The tag must match both pyproject.toml and Cargo.toml. PyPI publication uses the repository's PYPI_TOKEN secret.

License

Apache-2.0. See LICENSE.txt.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polodb_python-0.2.1.tar.gz (64.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

polodb_python-0.2.1-cp310-abi3-win_amd64.whl (3.9 MB view details)

Uploaded CPython 3.10+Windows x86-64

polodb_python-0.2.1-cp310-abi3-manylinux_2_28_x86_64.whl (4.7 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.28+ x86-64

polodb_python-0.2.1-cp310-abi3-manylinux_2_28_aarch64.whl (4.2 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.28+ ARM64

polodb_python-0.2.1-cp310-abi3-macosx_11_0_arm64.whl (3.5 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

polodb_python-0.2.1-cp310-abi3-macosx_10_13_x86_64.whl (3.8 MB view details)

Uploaded CPython 3.10+macOS 10.13+ x86-64

File details

Details for the file polodb_python-0.2.1.tar.gz.

File metadata

  • Download URL: polodb_python-0.2.1.tar.gz
  • Upload date:
  • Size: 64.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for polodb_python-0.2.1.tar.gz
Algorithm Hash digest
SHA256 9f264f3ed18b9c04f03b68e03c679277674f9ff87e6113b96e9df74a3bb7927f
MD5 9e29adc8f4d46780fa8ba63f4e104975
BLAKE2b-256 c29a2ce545cd30075e5e62f4b6881858f0325bc2d9886e180aded09a6ca50784

See more details on using hashes here.

File details

Details for the file polodb_python-0.2.1-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for polodb_python-0.2.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 8bc418122bd5de6c2d689bde986d494592f665a1737093fef2c3e1990d42e784
MD5 41e25335dd1710c92bacc3f3d9a477a5
BLAKE2b-256 cee560e25f128f5a6d06dae5408304ceb403cb20d66d00cd3b54c04554bb0725

See more details on using hashes here.

File details

Details for the file polodb_python-0.2.1-cp310-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for polodb_python-0.2.1-cp310-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5726a4567157010b10f0b2b298eb5f3fd7d6e2c8f10f84ba91be574d76849d79
MD5 b82c410277c9e1c02c927b7396cafcdd
BLAKE2b-256 48778b76d346f7a9c148849aae1863801a8aa8f4b2d5fa1e1bc98bc29e5479d0

See more details on using hashes here.

File details

Details for the file polodb_python-0.2.1-cp310-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for polodb_python-0.2.1-cp310-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 6844ad54299f0df159ab3dd2449fb291562f3a4a749595b9d7ea6fbef2a96198
MD5 e5931a69efbceec5f48c9a358458acab
BLAKE2b-256 fbb2eb8a9a1aaca75896d2666fe38c1df0b1c68872002cea1f92eff4568e2d00

See more details on using hashes here.

File details

Details for the file polodb_python-0.2.1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for polodb_python-0.2.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4df34f250cf19565ee8869572162d3b0fa9dfa5e0942c8ef51d2ef13cac9a65f
MD5 9a9cceb5d6e0ecebb72fc2b59533c8ff
BLAKE2b-256 6445ffe758793067680097fcb34809050467139be0283b22973022c95019e721

See more details on using hashes here.

File details

Details for the file polodb_python-0.2.1-cp310-abi3-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for polodb_python-0.2.1-cp310-abi3-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 0a9abe9a463725604064129eddc454c65efae919f335ea4b2635e870bc085eae
MD5 2804079a507d3c4a1ffa01c46c0c57e4
BLAKE2b-256 417b2021047f8a20def830afd04d985c600b3bf737b67ed24d49197d25c831f9

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.1 This release

6 files

0.2.0

6 files

0.1.18

10 files

0.1.17

4 files

0.1.16

2 files

0.1.13

2 files

0.1.12

6 files

0.1.10

2 files

0.1.9

2 files

0.1.8

3 files

0.1.7

1 file

0.1.6

2 files

0.1.5

3 files

0.1.4

2 files

0.1.3

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page