MongoDBQueryEngine

MongoDBQueryEngine(
    connection_string: str,
    llm: ForwardRef('LLM') | None = None,
    database_name: str | None = None,
    embedding_function: ForwardRef('BaseEmbedding') | Callable[..., Any] | None = None,
    embedding_model: ForwardRef('BaseEmbedding') | str | None = None,
    collection_name: str | None = None
)

A query engine backed by MongoDB Atlas that supports document insertion and querying.
This engine initializes a vector database, builds an index from input documents, and allows querying using the chat engine interface.
Attributes:
vector_db (MongoDBAtlasVectorDB): The MongoDB vector database instance.
vector_search_engine (MongoDBAtlasVectorSearch): The vector search engine.
storage_context (StorageContext): The storage context for the vector store.
index (Optional[VectorStoreIndex]): The index built from the documents.
Initializes a MongoDBQueryEngine instance.

Parameters:
NameDescription
connection_stringType: str
llmType: ForwardRef(‘LLM’) | None

Default: None
database_nameType: str | None

Default: None
embedding_functionType: ForwardRef(‘BaseEmbedding’) | Callable[…, Any] | None

Default: None
embedding_modelType: ForwardRef(‘BaseEmbedding’) | str | None

Default: None
collection_nameType: str | None

Default: None

Instance Methods

add_docs

add_docs(
    self,
    new_doc_dir: Path | str | None = None,
    new_doc_paths_or_urls: Sequence[Path | str] | None = None,
    *args: Any,
    **kwargs: Any
) -> None

Adds new documents to the existing vector store index.
This method validates that the index exists, loads documents from the specified directory or file paths, and inserts them into the vector store index.

Parameters:
NameDescription
new_doc_dirDirectory containing new documents.

Type: pathlib.Path | str | None

Default: None
new_doc_paths_or_urlsList of file paths or URLs for new documents.

Type: Sequence[pathlib.Path | str] | None

Default: None
*argsAdditional positional arguments.

Type: Any
**kwargsAdditional keyword arguments.

Type: Any

connect_db

connect_db(
    self,
    *args: Any,
    **kwargs: Any
) -> bool

Connects to the MongoDB database and initializes the query index from the existing collection.
This method verifies the existence of the collection, sets up the database connection, builds the vector store index, and pings the MongoDB server.
Returns:
bool: True if connection is successful; False otherwise.

Parameters:
NameDescription
*argsType: Any
**kwargsType: Any
Returns:
TypeDescription
boolbool: True if connection is successful; False otherwise.

get_collection_name

get_collection_name(self) -> str

Retrieves the name of the MongoDB collection.
Returns:
str: The collection name.
Raises:
ValueError: If the collection name is not set.

Returns:
TypeDescription
strstr: The collection name.

init_db

init_db(
    self,
    new_doc_dir: Path | str | None = None,
    new_doc_paths_or_urls: Sequence[Path | str] | None = None,
    *args: Any,
    **kwargs: Any
) -> bool

Initializes the MongoDB database by creating or overwriting the collection and indexing documents.
This method loads documents from a directory or provided file paths, sets up the database (optionally overwriting any existing collection), builds the vector store index, and inserts the documents.

Parameters:
NameDescription
new_doc_dirDirectory containing documents to be indexed.

Type: pathlib.Path | str | None

Default: None
new_doc_paths_or_urlsList of file paths or URLs for documents.

Type: Sequence[pathlib.Path | str] | None

Default: None
*argsAdditional positional arguments.

Type: Any
**kwargsAdditional keyword arguments.

Type: Any
Returns:
TypeDescription
boolbool: True if the database is successfully initialized; False otherwise.

query

query(
    self,
    question: str,
    *args: Any,
    **kwargs: Any
) -> Any

Queries the indexed documents using the provided question.
This method validates that the query index is initialized, creates a query engine from the vector store index, and executes the query. If the response is empty, a default reply is returned.

Parameters:
NameDescription
questionThe query question.

Type: str
*argsType: Any
**kwargsType: Any
Returns:
TypeDescription
AnyAny: The query response as a string, or a default reply if no results are found.