Skip to main content

MongoDBQueryEngine

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:

Instance Methods

add_docs

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:

connect_db

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: Returns:

get_collection_name

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

init_db

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: Returns:

query

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: Returns: