Integrate Atlas Vector Search with LangChain
On this page
You can integrate Atlas Vector Search with LangChain to build generative AI and RAG applications. This page provides an overview of the MongoDB LangChain Python integration and the different components you can use in your applications.
Note
For a full list of components and methods, see API reference.
For the JavaScript integration, see LangChain JS/TS.
Installation and Setup
To use Atlas Vector Search with LangChain, you must first install
the langchain-mongodb
package:
pip install langchain-mongodb
Vector Store
MongoDBAtlasVectorSearch
is a vector store
that allows you to store and retrieve vector embeddings from a
collection in Atlas. You can use this component to store
embeddings from your data and retrieve them using Atlas Vector Search.
This component requires an Atlas Vector Search Index.
Usage
The quickest way to instantiate your vector store is to use the connection string for your Atlas cluster or local deployment:
from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearch from langchain_voyageai import VoyageAIEmbeddings # Instantiate the vector store using your MongoDB connection string vector_store = MongoDBAtlasVectorSearch.from_connection_string( connection_string = "<connection-string>", # Atlas cluster or local deployment URI namespace = "<database-name>.<collection-name>", # Database and collection name embedding = VoyageAIEmbeddings(), # Embedding model to use index_name = "vector_index", # Name of the vector search index # Other optional parameters... )
The integration also supports other methods of instantiating the vector store:
Using the MongoDB client:
from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearch from langchain_voyageai import VoyageAIEmbeddings from pymongo import MongoClient # Connect to your Atlas cluster or local deployment client = MongoClient("<connection-string>") collection = client["<database-name>"]["<collection-name>"] # Instantiate the vector store vector_store = MongoDBAtlasVectorSearch( collection = collection, # Collection to store embeddings embedding = VoyageAIEmbeddings(), # Embedding model to use index_name = "vector_index" # Name of the vector search index # Other optional parameters... ) From documents that you've created:
from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearch from langchain_voyageai import VoyageAIEmbeddings from langchain_core.documents import Document from pymongo import MongoClient # Some documents to embed document_1 = Document(page_content="foo", metadata={"baz": "bar"}) document_2 = Document(page_content="thud", metadata={"bar": "baz"}) docs = [ document_1, document_2 ] # Connect to your Atlas cluster or local deployment client = MongoClient("<connection-string>") collection = client["<database-name>"]["<collection-name>"] # Create the vector store from documents vector_store = MongoDBAtlasVectorSearch.from_documents( documents = docs, # List of documents to embed embedding = VoyageAIEmbeddings(), # Embedding model to use collection = collection, # Collection to store embeddings index_name = "vector_index" # Name of the vector search index # Other optional parameters... )
Parameter | Necessity | Description | ||
---|---|---|---|---|
| Required | Specify the connection string for your Atlas cluster or local Atlas deployment. Your connection string should use the following format:
NoteEnsure that your connection string includes your database user's credentials. To learn more about finding your connection string, see Connect via Drivers. Your connection string should use the following format:
| ||
| Required | Specify the MongoDB namespace for which to store vector embeddings. For example, | ||
| Required | The embedding model to use. You can use any embedding model supported in LangChain. | ||
| Optional | Name of the vector search index in Atlas. Defaults to | ||
| Optional | Field name that contains the document text content. Defaults to | ||
| Optional | Field name that stores the embedding vector. Defaults to | ||
| Optional | Similarity function to use. Accepted values are | ||
| Optional | Number of vector dimensions. If you set this value and you don't have a vector search index on the collection, Atlas creates the index. | ||
| Optional | Flag that determines whether to automatically create the vector index
if it doesn't exist. Defaults to | ||
| Optional | Timeout in seconds to wait for an auto-created vector search index to be ready. | ||
| Optional | Additional parameters to pass to the vector store such as LangChain-specific parameters. |
Retrievers
LangChain retrievers are components that you use to get relevant documents from your vector stores. You can use LangChain's built-in retrievers or the following MongoDB retrievers to query and retrieve data from Atlas.
Vector Search Retriever
After instantiating Atlas as a vector store, you can use the vector store instance as a retriever to query your data using Atlas Vector Search.
Usage
from langchain_mongodb.vectorstores import MongoDBAtlasVectorSearch # Instantiate the vector store vector_store = MongoDBAtlasVectorSearch.from_connection_string( # Vector store arguments... ) # Use the vector store as a retriever retriever = vector_store.as_retriever() # Define your query query = "some search query" # Print results documents = retriever.invoke(query) for doc in documents: print(doc)
Full-Text Retriever
MongoDBAtlasFullTextSearchRetriever
is a retriever that
performs full-text search by using Atlas Search.
Specifically, it uses Lucene's standard BM25 algorithm.
This retriever requires an Atlas Search Index.
Usage
from langchain_mongodb.retrievers.full_text_search import MongoDBAtlasFullTextSearchRetriever # Connect to your Atlas cluster client = MongoClient("<connection-string>") collection = client["<database-name>"]["<collection-name>"] # Initialize the retriever retriever = MongoDBAtlasFullTextSearchRetriever( collection = collection, # MongoDB Collection in Atlas search_field = "<field-name>", # Name of the field to search search_index_name = "<index-name>" # Name of the search index ) # Define your query query = "some search query" # Print results documents = retriever.invoke(query) for doc in documents: print(doc)
Note
Hybrid Search Retriever
MongoDBAtlasHybridSearchRetriever
is a retriever
that combines vector search and full-text search results by using the
Reciprocal Rank Fusion (RRF) algorithm. To learn more, see How to Perform Hybrid Search.
This retriever requires an existing vector store, Atlas Vector Search Index, and Atlas Search Index.
Usage
from langchain_mongodb.retrievers.hybrid_search import MongoDBAtlasHybridSearchRetriever # Initialize the retriever retriever = MongoDBAtlasHybridSearchRetriever( vectorstore = <vector-store>, # Vector store instance search_index_name = "<index-name>", # Name of the Atlas Search index top_k = 5, # Number of documents to return fulltext_penalty = 60.0, # Penalty for full-text search vector_penalty = 60.0 # Penalty for vector search ) # Define your query query = "some search query" # Print results documents = retriever.invoke(query) for doc in documents: print(doc)
Parent Document Retriever
MongoDBAtlasParentDocumentRetriever
is a retriever that
queries smaller chunks first and then returns the
larger parent document to the LLM. This type of retrieval
is called parent document retrieval. Parent document
retrieval can improve the responses of your RAG agents
and applications by allowing for more granular searches on smaller chunks
while giving LLMs the full context of the parent document.
This retriever stores both the parent and child documents in a single MongoDB collection, which supports efficient retrieval by only having to compute and index the child documents' embeddings.
Under the hood, this retriever creates the following:
An instance of MongoDBAtlasVectorSearch to handle vector search queries to the child documents.
An instance of MongoDBDocStore to handle storing and retrieving the parent documents.
Usage
from langchain_mongodb.retrievers.parent_document import ParentDocumentRetriever from langchain_text_splitters import RecursiveCharacterTextSplitter from langchain_openai import OpenAIEmbeddings retriever = MongoDBAtlasParentDocumentRetriever.from_connection_string( connection_string = <connection-string>, # Atlas connection string embedding_model = OpenAIEmbeddings(), # Embedding model to use child_splitter = RecursiveCharacterTextSplitter(), # Text splitter to use database_name = <database-name>, # Database to store the collection collection_name = <collection-name>, # Collection to store the collection # Additional vector store or parent class arguments... ) # Define your query query = "some search query" # Print results documents = retriever.invoke(query) for doc in documents: print(doc)
GraphRAG
GraphRAG is an alternative approach to traditional RAG that structures data as a knowledge graph of entities and their relationships instead of as vector embeddings. While vector-based RAG finds documents that are semantically similar to the query, GraphRAG finds connected entities to the query and traverses the relationships in the graph to retrieve relevant information.
This approach is particularly useful for answering relationship-based questions like "What is the connection between Company A and Company B?" or "Who is Person X's manager?".
MongoDBGraphStore
is a component in the LangChain MongoDB integration
that allows you to implement GraphRAG by storing entities (nodes)
and their relationships (edges) in a MongoDB collection. This component
stores each entity as a document with relationship fields that
reference other documents in your collection. It executes queries using
the $graphLookup
aggregation stage.
Usage
from langchain_mongodb import MongoDBGraphStore from langchain_openai import ChatOpenAI # Initialize the graph store graph_store = MongoDBGraphStore( connection_string = "<connection-string>", # Atlas connection string or local deployment URI database_name = "<database-name>", # Database to store the graph collection_name = "<collection-name>", # Collection to store the graph entity_extraction_model = ChatOpenAI(), # LLM to extract entities from documents (e.g. OpenAI model) # Other optional parameters... ) # Add documents to the graph docs = [...] # Your documents graph_store.add_documents(docs) # Query the graph query = "Who is the CEO of MongoDB?" answer = graph_store.chat_response(query) print(answer.content)
LLM Caches
Caches are used to optimize LLM performance by storing repetitive responses for similar or repetitive queries to avoid recomputing them. MongoDB provides the following caches for your LangChain applications.
MongoDB Cache
MongoDBCache
allows you to store a basic cache in Atlas.
Usage
from langchain_mongodb import MongoDBCache from langchain_core.globals import set_llm_cache set_llm_cache(MongoDBCache( connection_string = "<connection-string>", # Atlas connection string database_name = "<database-name>", # Database to store the cache collection_name = "<collection-name>" # Collection to store the cache ))
Semantic Cache
Semantic caching is a more advanced form of caching that retrieves cached prompts based on the semantic similarity between the user input and cached results.
MongoDBAtlasSemanticCache
is a semantic cache that uses Atlas Vector Search
to retrieve the cached prompts. This component requires an
Atlas Vector Search index.
Usage
from langchain_mongodb import MongoDBAtlasSemanticCache from langchain_core.globals import set_llm_cache # Use some embedding model to generate embeddings from tests.integration_tests.vectorstores.fake_embeddings import FakeEmbeddings set_llm_cache(MongoDBAtlasSemanticCache( embedding = FakeEmbeddings(), # Embedding model to use connection_string = "<connection-string>", # Atlas connection string database_name = "<database-name>", # Database to store the cache collection_name = "<collection-name>" # Collection to store the cache ))
Document Loader
Document loaders are tools that help you to load data for your LangChain applications.
MongoDBLoader
is a document loader that returns a list of
documents from a MongoDB database.
Usage
from langchain_mongodb.loaders import MongoDBLoader loader = MongoDBLoader.from_connection_string( connection_string = "<connection-string>", # Atlas cluster or local deployment URI db_name = "<database-name>", # Database that contains the collection collection_name = "<collection-name>", # Collection to load documents from filter_criteria = { "field": "value" }, # Optional document to specify a filter field_names = ["<field-name>", "..." ], # Optional list of fields to include in document content metadata_names = ["<metadata-field>", "..."] # Optional metadata fields to extract ) docs = loader.load()
Note
Chat History
MongoDBChatMessageHistory
is a component that allows you to store and manage
chat message histories in a MongoDB database. It can
save both user and AI-generated messages associated with a unique session identifier.
This is useful for applications that require tracking
of interactions over time, such as chatbots.
Usage
from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory chat_message_history = MongoDBChatMessageHistory( session_id = "<session-id>", # Unique session identifier connection_string = "<connection-string>", # Atlas cluster or local deployment URI database_name = "<database-name>", # Database to store the chat history collection_name = "<collection-name>" # Collection to store the chat history ) chat_message_history.add_user_message("Hello") chat_message_history.add_ai_message("Hi")
chat_message_history.messages
[HumanMessage(content='Hello'), AIMessage(content='Hi')]
Storage
You can use the following custom data stores to manage and store data in MongoDB.
Document Store
MongoDBDocStore
is a custom key-value store that uses MongoDB to store
and manage documents. You can perform CRUD operations as you would on
any other MongoDB collection.
Usage
from pymongo import MongoClient from langchain_mongodb.docstores import MongoDBDocStore # Replace with your MongoDB connection string and namespace connection_string = "<connection-string>" namespace = "<database-name>.<collection-name>" # Initialize the MongoDBDocStore docstore = MongoDBDocStore.from_connection_string(connection_string, namespace)
Note
Binary Storage
MongoDBByteStore
is a custom datastore that uses MongoDB to store
and manage binary data, specifically data represented in bytes.
You can perform CRUD operations with key-value pairs where
the keys are strings and the values are byte sequences.
Usage
from langchain.storage import MongoDBByteStore # Instantiate the MongoDBByteStore mongodb_store = MongoDBByteStore( connection_string = "<connection-string>", # Atlas cluster or local deployment URI db_name = "<database-name>", # Name of the database collection_name = "<collection-name>" # Name of the collection ) # Set values for keys mongodb_store.mset([("key1", b"hello"), ("key2", b"world")]) # Get values for keys values = mongodb_store.mget(["key1", "key2"]) print(values) # Output: [b'hello', b'world'] # Iterate over keys for key in mongodb_store.yield_keys(): print(key) # Output: key1, key2 # Delete keys mongodb_store.mdelete(["key1", "key2"])
Note
Additional Resources
To learn how to integrate Atlas Vector Search with LangGraph, see Integrate MongoDB with LangGraph.
MongoDB also provides the following developer resources: