Database — Advanced Methods
Additionaldb 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.
None if not found.
count_documents()
Count documents matching optional filters without fetching the data.
search_documents()
Full-text search across a collection. Searches all text fields by default, or specific fields if you supply them.
fetch_all()
Run multiple independent queries in one call. Returns a list of results in the same order as the queries.
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.
None if not found.
App User CRUD
get_app_user()
Fetch a single app user by their ID.
None.
get_app_user_by_email()
Fetch a single app user by their email address.
None.
create_app_user()
Create a new app user programmatically (without going through the signup endpoint).
update_app_user()
Update an app user’s fields. Pass only the fields you want to change.
data is merged into the existing user data — you don’t need to include unchanged fields.delete_app_user()
Permanently delete an app user.
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.
{"data": [...], "total": N, "has_more": bool, ...}
How it works:
- If the user has a
{type}_idsarray in theirdata(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.
{"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.
{"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.
Never raises — always returns a plain dict.
- 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.
asyncdb.find_one(), asyncdb.create_document(), asyncdb.update_app_user(), etc.