Skip to main content
This notebook covers how to MongoDB Atlas vector search in LangChain, using the langchain-mongodb package.
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 Atlas 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.

Setup

*An Atlas cluster running MongoDB version 6.0.11, 7.0.2, or later (including RCs).
To use MongoDB Atlas, you must first deploy a cluster. We have a Forever-Free tier of cluster on a cloud of your choice available. To get started head over to Atlas here: quick start. You’ll need to install langchain-mongodb and pymongo to use this integration.

Credentials

For this notebook you will need to find your MongoDB cluster URI. For information on finding your cluster URI read through this guide.
If you want to get best in-class automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

Initialization

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

Manage vector store

Once you have created your vector store, we can interact with it by adding and deleting different items.

Add items to vector store

We can add items to our vector store by using the add_documents function.

Delete items from vector store

Query vector store

Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent.

Query directly

Performing a simple similarity search can be done as follows:

Similarity search with score

You can also search with score:
Atlas Vector Search supports pre-filtering using MQL Operators for filtering. Below is an example index and query on the same data loaded above that allows you do metadata filtering on the “page” field. You can update your existing index with the filter defined and do pre-filtering with vector search. To enable pre-filtering you need to update the index definition to include a filter field. In this example, we will use the source field as the filter field. This can be done programmatically using the MongoDBAtlasVectorSearch.create_vector_search_index method.
Alternatively, you can also update the index using the Atlas UI with the following index definition:
And then you can run a query with filter as follows:

Other search methods

There are a variety of other search methods that are not covered in this notebook, such as MMR search or searching by vector. For a full list of the search abilities available for MongoDBAtlasVectorStore check out the API reference.

Query by turning into retriever

You can also transform the vector store into a retriever for easier usage in your chains. Here is how to transform your vector store into a retriever and then invoke the retreiever with a simple query and filter.

Usage for retrieval-augmented generation

For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:

Other Notes

  • More documentation can be found at MongoDB’s LangChain Docs site
  • This feature is Generally Available and ready for production deployments.
  • The langchain version 0.0.305 (release notes) introduces the support for $vectorSearch MQL stage, which is available with MongoDB Atlas 6.0.11 and 7.0.2. Users utilizing earlier versions of MongoDB Atlas need to pin their LangChain version to <=0.0.304

API reference

For detailed documentation of all MongoDBAtlasVectorSearch features and configurations head to the API reference: python.langchain.com/api_reference/mongodb/index.html
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.