Skip to content

API Reference parrot.tools.databasequery.sources.mongodb

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.tools.databasequery.sources.mongodb

View source

MongoDB database source for DatabaseToolkit.

Implements AbstractDatabaseSource for MongoDB using the asyncdb mongo driver. Overrides validate_query() with JSON-based validation (filter documents and aggregation pipelines). Discovers schema via collection listing and $sample aggregation for field inference.

This is the base class for DocumentDBSource and AtlasSource.

Part of FEAT-062 — DatabaseToolkit.

Alias AbstractDatabaseSource

Re-exported from parrot.tools.databasequery.base.AbstractDatabaseSource.

Alias ColumnMeta

Re-exported from parrot.tools.databasequery.base.ColumnMeta.

Alias MetadataResult

Re-exported from parrot.tools.databasequery.base.MetadataResult.

Class MongoSource

MongoDB database source.

Uses the asyncdb mongo driver with JSON-based query validation. Supports both filter-only queries and command-style queries.

Method MongoSource.get_default_credentials

Return default MongoDB credentials from environment variables.

Reads MONGODB_HOST, MONGODB_PORT, MONGODB_DATABASE, MONGODB_USER, MONGODB_PASSWORD from navconfig via the expanded interface function. Returns a dict with dbtype: "mongodb" already set for asyncdb driver-type disambiguation.

get_default_credentials() -> dict[str, Any]

Returns: dict[str, Any] — Dict with MongoDB connection parameters, or empty dict if no env vars are configured.

Method MongoSource.validate_query

Validate a MongoDB query string (JSON format).

Accepts:

  • A JSON object (filter document): {"status": "active"}
  • A JSON array of objects (aggregation pipeline): [{"$match": {...}}, {"$group": {...}}]

validate_query(query: str) -> ValidationResult

Parameter Type Default Description
query str JSON string representing a filter or pipeline.

Returns: ValidationResult — ValidationResult indicating whether the query is valid JSON in the expected format.

Method MongoSource.test_connection

Test MongoDB connectivity using the ping command.

Overrides the base class SELECT 1 default because MongoDB does not support SQL. Runs a ping command against the server using the asyncdb mongo driver connection.

test_connection(credentials: dict[str, Any]) -> bool

Parameter Type Default Description
credentials dict[str, Any] Connection credentials for the MongoDB server.

Returns: bool — True if the ping succeeds, False on any exception. Never raises.

Method MongoSource.get_metadata

Discover MongoDB schema: collections and inferred field types.

Lists all collection names and uses $sample to infer field names and types from a small sample of documents.

get_metadata(credentials: dict[str, Any], tables: list[str] | None = None) -> MetadataResult

Parameter Type Default Description
credentials dict[str, Any] Connection credentials (including database key).
tables list[str] | None None Optional list of collection names to inspect.

Returns: MetadataResult — MetadataResult with collections as TableMeta and inferred fields as ColumnMeta.

Method MongoSource.query

Execute a MongoDB query (JSON filter or command).

Supports two query forms:

  • Filter-only: {"status": "active"} — uses collection_name from credentials.
  • Command-style: {"find": "users", "filter": {...}, "limit": 10}

query(credentials: dict[str, Any], sql: str, params: dict[str, Any] | None = None) -> QueryResult

Parameter Type Default Description
credentials dict[str, Any] Connection credentials (must include collection_name for filter-only queries, or database for command-style).
sql str JSON query string (filter document or command).
params dict[str, Any] | None None Ignored for MongoDB (filters are embedded in the query).

Returns: QueryResult — QueryResult with documents and execution metadata.

Method MongoSource.query_row

Execute a MongoDB query and return a single document.

query_row(credentials: dict[str, Any], sql: str, params: dict[str, Any] | None = None) -> RowResult

Parameter Type Default Description
credentials dict[str, Any] Connection credentials.
sql str JSON filter document.
params dict[str, Any] | None None Ignored for MongoDB.

Returns: RowResult — RowResult with a single document or found=False.

Alias QueryResult

Re-exported from parrot.tools.databasequery.base.QueryResult.

Alias RowResult

Re-exported from parrot.tools.databasequery.base.RowResult.

Alias TableMeta

Re-exported from parrot.tools.databasequery.base.TableMeta.

Alias ValidationResult

Re-exported from parrot.tools.databasequery.base.ValidationResult.

Alias register_source

Re-exported from parrot.tools.databasequery.sources.register_source.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally