Skip to main content
This notebook covers how to use MongoDB Vector Search with LangChain. It also showcases the MongoDB Atlas Embedding and Reranking API for accessing Voyage AI’s state-of-the-art embedding models and rerankers.
MongoDB Atlas is a fully-managed cloud database available in AWS, Azure, and GCP. It supports native Vector Search, full text search (BM25), and hybrid search on your MongoDB document data.
MongoDB Vector Search allows to store your embeddings in MongoDB documents, create a vector search index, and perform KNN search with an approximate nearest neighbor algorithm (Hierarchical Navigable Small Worlds). It uses the $vectorSearch MQL Stage.
MongoDB Atlas Embedding and Reranking API enables access to Voyage AI models via MongoDB Atlas.
Note: The MongoDBAtlasVectorSearch interface is compatible with MongoDB Vector Search in the Community Edition.

Setup

To use MongoDB Atlas, you must first deploy a cluster. To get started, sign up for free to Atlas. In order to use Voyage AI embedding and reranking models, you will need to create a model API key. Generate your API key, and get access to 200 million free tokens on the latest models. First, start by installing the following libraries to follow this notebook.
The langchain-community package is no longer maintained. Examples that import from langchain_community may be outdated or broken. Use with caution.
Replace the placeholder values in the following code cell with your Atlas Cluster connection string and your Model API Key.

Insert data

We load documents, generate embeddings via the Atlas-hosted Voyage AI model, and programmatically create the Vector Search index.

Create vector index

[OPTIONAL] Alternative to the vector_store.create_vector_search_index command above, you can also create the vector search index using the Atlas UI with the following index definition:

Query vector store

Find the most relevant documents based on semantic similarity.

Semantic search with score

Retrieve documents along with their relevance scores.

Semantic search with filtering

First, let’s update the vector search index by providing the field to filter on.
Narrow down results using metadata filters. Note that Atlas Vector Search requires explicit operators like $eq.
Combining Vector Search with Full-Text Search (Keyword) using Reciprocal Rank Fusion (RRF).

Vector search and reranking

A two-stage process: broad recall followed by high-precision reranking to ensure maximum relevance.

Retrieval-Augmented Generation (RAG)

For guides on how to use the MongoDB Vector Store integration with LangChain for Retrieval-Augmented Generation (RAG), see the following tutorials:

Other notes

  • More documentation can be found at MongoDB’s LangChain Docs.
  • This feature is Generally Available and ready for production deployments.

API reference

For detailed documentation of all MongoDBAtlasVectorSearch features and configurations head to the API reference.