MongoDBAtlasVectorSearch features and configurations head to the API reference.
Overview
Integration details
Setup
To use MongoDB Atlas vector stores, you’ll need to configure a MongoDB Atlas cluster and install the@langchain/mongodb integration package.
Initial Cluster Configuration
To create a MongoDB Atlas cluster, navigate to the MongoDB Atlas website and create an account if you don’t already have one. Create and name a cluster when prompted, then find it underDatabase. Select Browse Collections and create either a blank collection or one from the provided sample data.
Note: The cluster must be MongoDB 7.0 or higher for manual embedding mode. Automated embedding mode requires MongoDB 8.2 or higher.
Creating a Vector Search Index
After configuring your cluster, create a vector search index on your collection. You can do this either on Atlas, Compass, or MongoDB Shell. The index definition depends on which embedding mode you use. Manual embedding (MongoDB 7.0+): you embed documents client-side and store the vectors in a field. Use the following definition, adjustingnumDimensions to match your embeddings model.
autoEmbed field type and specify the model:
text and (in manual mode) writes vectors to a field named embedding. Set textKey and embeddingKey to match your index.
Embeddings
In manual embedding mode, you provide an embeddings model and embed documents client-side. This guide uses OpenAI embeddings as an example. You can also use other supported embeddings models. In automated embedding mode, MongoDB Atlas handles embedding generation server-side. No client-side embeddings package is required.Installation
- Manual embedding
- Automated embedding
Install the core package plus an embeddings provider:
Credentials
Once you’ve done the above, set theMONGODB_ATLAS_URI environment variable from the Connect button in Mongo’s dashboard. You’ll also need your DB name and collection name:
Instantiation
Once you’ve set up your cluster and index, initialize your vector store. The constructor accepts two forms depending on whether you use manual or automated embedding.- Manual embedding
- Automated embedding
Pass an embeddings instance as the first argument:
Manage vector store
Add items to vector store
You can now add documents to your vector store:After adding documents, there is a delay before they become queryable. In automated embedding mode this delay is longer because MongoDB must generate embeddings server-side after insertion. Wait until the Atlas search index reports that documents are indexed before querying.
id as an existing document will update the existing one.
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:Filtering
MongoDB Atlas supports pre-filtering of results on other fields. They require you to define which metadata fields you plan to filter on by updating the index you created initially. Here’s an example:fields is the vector index, and the second item is the metadata property you want to filter on. The name of the property is the value of the path key. Therefore the above index allows us to search on a metadata field named source.
Then, in your code you can use MQL Query Operators for filtering.
The below example illustrates this:
Returning scores
If you want to execute a similarity search and receive the corresponding scores you can run:Query by turning into retriever
You can also transform the vector store into a retriever for easier usage in your chains.Usage for retrieval-augmented generation
For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:Closing connections
Make sure you close the client instance when you are finished to avoid excessive resource consumption:API reference
For detailed documentation of allMongoDBAtlasVectorSearch features and configurations head to the API reference.
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

