Bug description The MongoDB integration has a bug when there is already an existing index.

Environment - java version: java 21.0.2 2024-01-16 LTS - maven version: Apache Maven 3.9.6 - SpringAI version: 1.0.0-SNAPSHOT - Vector Store: MongoDB Atlas Vector Store - Dependency:

        <dependency>
            <groupId>[org.springframework.ai](http://org.springframework.ai/)</groupId>
            <artifactId>spring-ai-mongodb-atlas-store</artifactId>
        </dependency>

Steps to reproduce - setup project as described in Spring AI Documentation. - Add spring-ai-mongodb-atlas-store dependency. - Create Config Class as below:

package com.mongodb.springaimongodbmanual.config;
import [org.springframework.ai](http://org.springframework.ai/).embedding.EmbeddingModel;
import [org.springframework.ai](http://org.springframework.ai/).openai.OpenAiEmbeddingModel;
import [org.springframework.ai](http://org.springframework.ai/).openai.api.OpenAiApi;
import [org.springframework.ai](http://org.springframework.ai/).vectorstore.MongoDBAtlasVectorStore;
import [org.springframework.ai](http://org.springframework.ai/).vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
import com.mongodb.client.MongoClient;
@Configuration
@SpringBootConfiguration
@EnableAutoConfiguration
public class MongodbConfig {
    @Value("${[spring.ai](http://spring.ai/).openai.api-key}")
    private String openAiKey;
    @Value("${spring.data.mongodb.database}")
    private String databaseName;
    @Value("${spring.ai.vectorstore.mongodb.collection-name:vector_store}")
    private String collectionName;
    @Value("${spring.ai.vectorstore.mongodb.indexName:vector_index}")
    private String indexName;
    @Bean
    public VectorStore mongodbVectorStore(MongoTemplate mongoTemplate, EmbeddingModel embeddingModel) {
        return new MongoDBAtlasVectorStore(mongoTemplate, embeddingModel, MongoDBAtlasVectorStore.MongoDBVectorStoreConfig.builder()
            .build(), true);
    }
    @Bean
    public MongoTemplate mongoTemplate(MongoClient mongoClient) {
        return new MongoTemplate(mongoClient, databaseName);
    }
    @Bean
    public EmbeddingModel embeddingModel() {
        return new OpenAiEmbeddingModel(new OpenAiApi(openAiKey));
    }
}
  • Compile then run the application.
    • Error 68: Index already exists
  • Traced back to MongoDBAtlasVectorStore.java, afterPropertiesSet(), line 93-94
// Create search index, command doesn't do anything if already existing
mongoTemplate.executeCommand(createSearchIndex());

Expected behavior If there is already an index configured, we would expect the integration to not create another index (in line with commented text above this line)

Minimal Complete Reproducible example