Skip to main content

Database — Advanced Methods

Additional db methods not covered in the introduction.

update_document_fields()

Merge specific fields into a document without overwriting the rest. Safer than update_document() when you only need to change a few keys.
Returns: Updated document dict, or None if not found.

count_documents()

Count documents matching optional filters without fetching the data.
Returns: Integer count.

search_documents()

Full-text search across a collection. Searches all text fields by default, or specific fields if you supply them.
Returns: List of matching document dicts (not a paginated result — just a plain list).

fetch_all()

Run multiple independent queries in one call. Returns a list of results in the same order as the queries.
Each query dict must have a type key: For "query" and "find_one", also include "collection": "<name>". All other kwargs are passed as filters. Returns: List where each element is the result of the corresponding query.

get_document()

Get a single document by ID.
Returns: Document dict, or None if not found.

App User CRUD

get_app_user()

Fetch a single app user by their ID.
Returns: User dict or None.

get_app_user_by_email()

Fetch a single app user by their email address.
Returns: User dict or None.

create_app_user()

Create a new app user programmatically (without going through the signup endpoint).
Returns: Created user dict.

update_app_user()

Update an app user’s fields. Pass only the fields you want to change.
Returns: Updated user dict.
data is merged into the existing user data — you don’t need to include unchanged fields.

delete_app_user()

Permanently delete an app user.
Returns: True if deleted, False if not found.

count_app_users()

Return the total number of app users in the project.

User Relationships

get_user_relationships()

Get users related to a user — followers, following, friends, teammates, etc.
Returns: {"data": [...], "total": N, "has_more": bool, ...} How it works:
  • If the user has a {type}_ids array in their data (e.g. following_ids), it fetches those users directly.
  • If not, it does a reverse lookup — finds users who have this user in their corresponding array.

add_user_relationship()

Add a relationship between two users. Updates the {type}_ids array in user data.
Returns: {"success": True, "message": "...", "bidirectional": bool}

remove_user_relationship()

Remove a relationship between two users.

get_user_collections()

Get documents from a collection that belong to a specific user. Automatically matches on author_id, user_id, creator_id, or owner_id.
Returns: {"data": [...], "total": N, "has_more": bool, ...}

transaction()

Execute a function exactly once with atomic locking and idempotency. Protects against duplicate runs, race conditions, and double-charges.
Returns dict: Never raises — always returns a plain dict.
When to use it:
  • Payment processing — prevent double charges on retried requests
  • Inventory deduction — prevent overselling under concurrent requests
  • Credit/token spending — protect balances from race conditions
  • Any operation where running twice would cause data corruption

Collection Management

You can also create, update, and delete collections from cloud functions (rarely needed, but useful for dynamic schemas):

asyncdb

asyncdb is the async version of db — identical methods, but awaitable. Use it inside async def main() for concurrent queries.
All the same methods are available — asyncdb.find_one(), asyncdb.create_document(), asyncdb.update_app_user(), etc.