Use this file to discover all available pages before exploring further.
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.
This guide provides a quick overview for getting started with MongoDB Atlas vector stores. For detailed documentation of all MongoDBAtlasVectorSearch features and configurations head to the API reference.
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 under Database. 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.
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, adjusting numDimensions to match your embeddings model.
Automated embedding (MongoDB 8.2+): MongoDB generates embeddings server-side using Voyage AI models. Use the autoEmbed field type and specify the model:
By default, the vector store reads from a text field named text and (in manual mode) writes vectors to a field named embedding. Set textKey and embeddingKey to match your index.
const vectorStore = new MongoDBAtlasVectorSearch( embeddings, // omit in auto embedding mode { collection, indexName: "index_name", textKey: "textContent", // document field where raw text is stored embeddingKey: "embedding", // matches "path" (omit in auto embedding mode) });
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.
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 manual embedding mode with OpenAI, set your OpenAI key as well:
process.env.OPENAI_API_KEY = "YOUR_API_KEY";
In automated embedding mode, no additional API key is required — MongoDB Atlas handles embedding generation using the model configured in your index.如果你想要自动追踪模型调用,还可以设置你的 LangSmith API 密钥,取消注释以下内容:
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:
import { MongoDBAtlasVectorSearch } from "@langchain/mongodb";import { OpenAIEmbeddings } from "@langchain/openai";import { MongoClient } from "mongodb";const client = new MongoClient(process.env.MONGODB_ATLAS_URI!);const collection = client .db(process.env.MONGODB_ATLAS_DB_NAME) .collection(process.env.MONGODB_ATLAS_COLLECTION_NAME);const embeddings = new OpenAIEmbeddings({ model: "text-embedding-3-small",});const vectorStore = new MongoDBAtlasVectorSearch(embeddings, { collection, indexName: "vector_index", // Defaults to "default" textKey: "text", // Defaults to "text" embeddingKey: "embedding", // Defaults to "embedding"});
Pass only the configuration object (no embeddings argument is needed).
import { MongoDBAtlasVectorSearch } from "@langchain/mongodb";import { MongoClient } from "mongodb";const client = new MongoClient(process.env.MONGODB_ATLAS_URI!);const collection = client .db(process.env.MONGODB_ATLAS_DB_NAME) .collection(process.env.MONGODB_ATLAS_COLLECTION_NAME);const vectorStore = new MongoDBAtlasVectorSearch({ collection, indexName: "default", // Must match the index name in your Atlas cluster});
import type { Document } from "@langchain/core/documents";const document1: Document = { pageContent: "The powerhouse of the cell is the mitochondria", metadata: { source: "https://example.com" }};const document2: Document = { pageContent: "Buildings are made out of brick", metadata: { source: "https://example.com" }};const document3: Document = { pageContent: "Mitochondria are made out of lipids", metadata: { source: "https://example.com" }};const document4: Document = { pageContent: "The 2024 Olympics are in Paris", metadata: { source: "https://example.com" }}const documents = [document1, document2, document3, document4];await vectorStore.addDocuments(documents, { ids: ["1", "2", "3", "4"] });
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.
Adding a document with the same id as an existing document will update the existing one.
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.
* The powerhouse of the cell is the mitochondria [{"_id":"1","source":"https://example.com"}]* Mitochondria are made out of lipids [{"_id":"3","source":"https://example.com"}]
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:
* The powerhouse of the cell is the mitochondria [{"_id":"1","source":"https://example.com"}]* Mitochondria are made out of lipids [{"_id":"3","source":"https://example.com"}]
* [SIM=0.374] The powerhouse of the cell is the mitochondria [{"_id":"1","source":"https://example.com"}]* [SIM=0.370] Mitochondria are made out of lipids [{"_id":"3","source":"https://example.com"}]
[ Document { pageContent: 'The powerhouse of the cell is the mitochondria', metadata: { _id: '1', source: 'https://example.com' }, id: undefined }, Document { pageContent: 'Mitochondria are made out of lipids', metadata: { _id: '3', source: 'https://example.com' }, id: undefined }]