MongoDB Atlas

WIP: Please consider contributing docs: Add documentation for the MongoDB Atlas Vector Store

MongoDB Atlas Vector Search is a fully managed cloud database service that provides the easiest way to deploy, operate, and scale a MongoDB database in the cloud.

Prerequisites

TODO: Add prerequisites instructions

Auto-configuration

Spring AI provides Spring Boot auto-configuration for the MongoDB Atlas Vector Store. To enable it, add the following dependency to your project’s Maven pom.xml file:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-mongodb-atlas-store-spring-boot-starter</artifactId>
</dependency>

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-mongodb-atlas-store-spring-boot-starter'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file.
Refer to the Repositories section to add Milestone and/or Snapshot Repositories to your build file.

The vector store implementation can initialize the requisite schema for you, but you must opt-in by specifying the initializeSchema boolean in the appropriate constructor or by setting …​initialize-schema=true in the application.properties file.

this is a breaking change! In earlier versions of Spring AI, this schema initialization happened by default.

Additionally, you will need a configured EmbeddingModel bean. Refer to the EmbeddingModel section for more information.

Here is an example of the needed bean:

@Bean
public EmbeddingModel embeddingModel() {
    // Can be any other EmbeddingModel implementation.
    return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("SPRING_AI_OPENAI_API_KEY")));
}

Metadata filtering

You can leverage the generic, portable metadata filters with MongoDB Atlas store as well.

For example, you can use either the text expression language:

vectorStore.similaritySearch(
                    SearchRequest.defaults()
                            .withQuery("The World")
                            .withTopK(TOP_K)
                            .withSimilarityThreshold(SIMILARITY_THRESHOLD)
                            .withFilterExpression("author in ['john', 'jill'] && 'article_type' == 'blog'"));

or programmatically using the Filter.Expression DSL:

FilterExpressionBuilder b = new FilterExpressionBuilder();

vectorStore.similaritySearch(SearchRequest.defaults()
                    .withQuery("The World")
                    .withTopK(TOP_K)
                    .withSimilarityThreshold(SIMILARITY_THRESHOLD)
                    .withFilterExpression(b.and(
                            b.in("author", "john", "jill"),
                            b.eq("article_type", "blog")).build()));
Those (portable) filter expressions get automatically converted into the proprietary MongoDB Atlas filter expressions.

MongoDB Atlas properties

You can use the following properties in your Spring Boot configuration to customize the MongoDB Atlas vector store.

Property Description Default value

spring.ai.vectorstore.mongodb.collection-name

The name of the collection to store the vectors.

vector_store

spring.ai.vectorstore.mongodb.initialize-schema

whether to initialize the backend schema for you

false

spring.ai.vectorstore.mongodb.path-name

The name of the path to store the vectors.

embedding

spring.ai.vectorstore.mongodb.indexName

The name of the index to store the vectors.

vector_index