Beanie

2.0.1 · active · verified Tue Mar 24

Async Python ODM for MongoDB built on Pydantic. Current version: 2.0.1 (Nov 2025). v2.0 breaking change: dropped Motor in favor of pymongo AsyncMongoClient. Requires Pydantic v2. Inner class Collection removed — use Settings instead. Must call init_beanie() before any document operations. Sync version is Bunnet (separate package).

Warnings

Install

Imports

Quickstart

Beanie v2.x quickstart with pymongo AsyncMongoClient.

# pip install beanie
from pymongo import AsyncMongoClient
from beanie import Document, Indexed, init_beanie
from typing import Optional
import asyncio

class Product(Document):
    name: str
    price: Indexed(float)
    in_stock: bool = True

    class Settings:
        name = 'products'  # MongoDB collection name

async def main():
    client = AsyncMongoClient('mongodb://localhost:27017')
    await init_beanie(database=client.shop, document_models=[Product])

    # Insert
    p = await Product(name='Widget', price=9.99).insert()
    print(p.id)  # ObjectId

    # Query
    cheap = await Product.find(Product.price < 20.0).to_list()
    one = await Product.find_one(Product.name == 'Widget')

    # Update
    await one.set({Product.price: 11.99})

    # Delete
    await one.delete()

asyncio.run(main())

view raw JSON →