MongodbLoader#

class langchain_community.document_loaders.mongodb.MongodbLoader(connection_string: str, db_name: str, collection_name: str, *, filter_criteria: Dict | None = None, field_names: Sequence[str] | None = None, metadata_names: Sequence[str] | None = None, include_db_collection_in_metadata: bool = True)[source]#

Load MongoDB documents.

Initializes the MongoDB loader with necessary database connection details and configurations.

Parameters:
  • connection_string (str) – MongoDB connection URI.

  • db_name (str) – Name of the database to connect to.

  • collection_name (str) – Name of the collection to fetch documents from.

  • filter_criteria (Optional[Dict]) – MongoDB filter criteria for querying

  • documents. (extract from)

  • field_names (Optional[Sequence[str]]) – List of field names to retrieve

  • documents.

  • metadata_names (Optional[Sequence[str]]) – Additional metadata fields to

  • documents.

  • include_db_collection_in_metadata (bool) – Flag to include database and

  • metadata. (collection names in)

Raises:
  • ImportError – If the motor library is not installed.

  • ValueError – If any necessary argument is missing.

Methods

__init__(connection_string, db_name, ...[, ...])

Initializes the MongoDB loader with necessary database connection details and configurations.

alazy_load()

A lazy loader for Documents.

aload()

Asynchronously loads data into Document objects.

lazy_load()

A lazy loader for Documents.

load()

Load data into Document objects.

load_and_split([text_splitter])

Load Documents and split into chunks.

__init__(connection_string: str, db_name: str, collection_name: str, *, filter_criteria: Dict | None = None, field_names: Sequence[str] | None = None, metadata_names: Sequence[str] | None = None, include_db_collection_in_metadata: bool = True) → None[source]#

Initializes the MongoDB loader with necessary database connection details and configurations.

Parameters:
  • connection_string (str) – MongoDB connection URI.

  • db_name (str) – Name of the database to connect to.

  • collection_name (str) – Name of the collection to fetch documents from.

  • filter_criteria (Optional[Dict]) – MongoDB filter criteria for querying

  • documents. (extract from)

  • field_names (Optional[Sequence[str]]) – List of field names to retrieve

  • documents.

  • metadata_names (Optional[Sequence[str]]) – Additional metadata fields to

  • documents.

  • include_db_collection_in_metadata (bool) – Flag to include database and

  • metadata. (collection names in)

Raises:
  • ImportError – If the motor library is not installed.

  • ValueError – If any necessary argument is missing.

Return type:

None

async alazy_load() → AsyncIterator[Document]#

A lazy loader for Documents.

Return type:

AsyncIterator[Document]

async aload() → List[Document][source]#

Asynchronously loads data into Document objects.

Return type:

List[Document]

lazy_load() → Iterator[Document]#

A lazy loader for Documents.

Return type:

Iterator[Document]

load() → List[Document][source]#

Load data into Document objects.

Attention:

This implementation starts an asyncio event loop which will only work if running in a sync env. In an async env, it should fail since there is already an event loop running.

This code should be updated to kick off the event loop from a separate thread if running within an async context.

Return type:

List[Document]

load_and_split(text_splitter: TextSplitter | None = None) → list[Document]#

Load Documents and split into chunks. Chunks are returned as Documents.

Do not override this method. It should be considered to be deprecated!

Parameters:

text_splitter (Optional[TextSplitter]) – TextSplitter instance to use for splitting documents. Defaults to RecursiveCharacterTextSplitter.

Returns:

List of Documents.

Return type:

list[Document]

Examples using MongodbLoader