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.Initialization
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 theadd_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
Similarity search
Performing a simple similarity search can be done as follows:Similarity search with score
You can also search with score:Pre-filtering with Similarity Search
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 thesource
field as the filter field.
This can be done programmatically using the MongoDBAtlasVectorSearch.create_vector_search_index
method.
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 forMongoDBAtlasVectorStore
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 allMongoDBAtlasVectorSearch
features and configurations head to the API reference: python.langchain.com/api_reference/mongodb/index.html