Initializing...

CRUD & Bulk Operations

Core data manipulation methods.

INSERT Insertions

Single Document

result = await collection.insert_one({
    "name": "Project X",
    "status": "active"
})
print(result.inserted_id)

Batch Insert (insert_many)

Efficiently insert multiple documents in a single network round-trip.

docs = [
    {"name": "Alice", "role": "dev"},
    {"name": "Bob", "role": "admin"}
]

ids = await collection.insert_many(docs, ordered=True)
print(f"Inserted {len(ids)} documents")

FIND Querying Data

# Find active projects with priority > 3
cursor = await collection.find(
    {"status": "active", "priority": {"$gt": 3}},
    sort=[["priority", -1]], 
    limit=20
)

for doc in cursor.documents:
    print(doc)

UPDATE Updates

# Update One
await collection.update_one(
    {"_id": "doc_123"},
    {"$set": {"status": "completed"}}
)

# Update Many
result = await collection.update_many(
    {"status": "pending"},
    {"$set": {"status": "archived"}}
)
print(result.modified_count)

BATCH Bulk Write Operations

Execute a mixed list of write operations (insert, update, delete) in a single batch.

ops = [ {"action": "insert", "document": {"name": "New Item"}}, { "action": "update", "query": {"name": "Old Item"}, "update": {"$set": {"active": False}} }, {"action": "delete", "query": {"status": "banned"}} ] # Atomic = True means all succeed or all fail result = await collection.bulk_write(ops, atomic=True) print(result) # {'inserted_ids': [...], 'modified_count': 1, 'deleted_count': 5}

Last updated: Dec 14, 2025