MongoDBDatabase#

class langchain_mongodb.agent_toolkit.database.MongoDBDatabase(client: MongoClient, database: str, schema: str | None = None, ignore_collections: List[str] | None = None, include_collections: List[str] | None = None, sample_docs_in_collection_info: int = 3, indexes_in_collection_info: bool = False)[source]#

Wrapper around a MongoDB database.

Methods

__init__(client, database[, schema, ...])

Create a MongoDBDatabase from client and database name.

close()

Close the resources used by the MongoDBDatabase.

from_connection_string(connection_string[, ...])

Construct a MongoDBDatabase from URI.

get_collection_info([collection_names])

Get information about specified collections.

get_collection_info_no_throw([collection_names])

Get information about specified collections.

get_context()

Return db context that you may want in agent prompt.

get_usable_collection_names()

Get names of collections available.

run(command)

Execute a MongoDB aggregation command and return a string representing the results.

run_no_throw(command)

Execute a MongoDB command and return a string representing the results.

Parameters:
  • client (MongoClient)

  • database (str)

  • schema (Optional[str])

  • ignore_collections (Optional[List[str]])

  • include_collections (Optional[List[str]])

  • sample_docs_in_collection_info (int)

  • indexes_in_collection_info (bool)

__init__(client: MongoClient, database: str, schema: str | None = None, ignore_collections: List[str] | None = None, include_collections: List[str] | None = None, sample_docs_in_collection_info: int = 3, indexes_in_collection_info: bool = False)[source]#

Create a MongoDBDatabase from client and database name.

Parameters:
  • client (MongoClient)

  • database (str)

  • schema (str | None)

  • ignore_collections (List[str] | None)

  • include_collections (List[str] | None)

  • sample_docs_in_collection_info (int)

  • indexes_in_collection_info (bool)

close() None[source]#

Close the resources used by the MongoDBDatabase.

Return type:

None

classmethod from_connection_string(connection_string: str, database: str | None = None, **kwargs: Any) MongoDBDatabase[source]#

Construct a MongoDBDatabase from URI.

Parameters:
  • connection_string (str)

  • database (str | None)

  • kwargs (Any)

Return type:

MongoDBDatabase

get_collection_info(collection_names: List[str] | None = None) str[source]#

Get information about specified collections.

Follows best practices as specified in: Rajkumar et al, 2022 (https://arxiv.org/abs/2204.00498)

If sample_rows_in_collection_info, the specified number of sample rows will be appended to each collection description. This can increase performance as demonstrated in the paper.

Parameters:

collection_names (List[str] | None)

Return type:

str

get_collection_info_no_throw(collection_names: List[str] | None = None) str[source]#

Get information about specified collections.

Follows best practices as specified in: Rajkumar et al, 2022 (https://arxiv.org/abs/2204.00498)

If sample_rows_in_collection_info, the specified number of sample rows will be appended to each collection description. This can increase performance as demonstrated in the paper.

Parameters:

collection_names (List[str] | None)

Return type:

str

get_context() Dict[str, Any][source]#

Return db context that you may want in agent prompt.

Return type:

Dict[str, Any]

get_usable_collection_names() Iterable[str][source]#

Get names of collections available.

Return type:

Iterable[str]

run(command: str) str | Cursor[source]#

Execute a MongoDB aggregation command and return a string representing the results.

If the statement returns documents, a string of the results is returned. If the statement returns no documents, an empty string is returned.

The command MUST be of the form: db.collectionName.aggregate(…).

Parameters:

command (str)

Return type:

str | Cursor

run_no_throw(command: str) str | Cursor[source]#

Execute a MongoDB command and return a string representing the results.

If the statement returns rows, a string of the results is returned. If the statement returns no rows, an empty string is returned.

If the statement throws an error, the error message is returned.

Parameters:

command (str)

Return type:

str | Cursor