Skip to main content
Compatibility: Only available on Node.js.You can still create API routes that use MongoDB with Next.js by setting the runtime variable to nodejs like so:export const runtime = "nodejs";For more information, see Edge runtimes in the Next.js documentation.
Note that the dimensions property should match the dimensionality of the embeddings you are using. For example, Cohere embeddings have 1024 dimensions, and by default OpenAI embeddings have 1536: Note: By default the vector store expects an index name of default, an indexed collection field name of embedding, and a raw text field name of text. You should initialize the vector store with field names matching your index name collection schema as shown below. Finally, proceed to build the index.

Embeddings

This guide will also use OpenAI embeddings, which require you to install the @langchain/openai integration package. You can also use other supported embeddings models if you wish.

Installation

Install the following packages:

Credentials

Once you’ve done the above, set the MONGODB_ATLAS_URI environment variable from the Connect button in Mongo’s dashboard. You’ll also need your DB name and collection name:
If you are using OpenAI embeddings for this guide, you’ll need to set your OpenAI key as well:
If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

Instantiation

Once you’ve set up your cluster as shown above, you can initialize your vector store as follows:

Manage vector store

Add items to vector store

You can now add documents to your vector store:
Note: After adding documents, there is a slight delay before they become queryable. Adding a document with the same 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:
Above, the first item in 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 all MongoDBAtlasVectorSearch features and configurations head to the API reference.