TTL Index Not Created in MongoDB Chat Memory Repository
Bug description
The property spring.ai.chat.memory.repository.mongo.create-indices = true does not trigger the creation of indices in Spring AI for the MongoDB chat memory repository. In particular, the TTL index (and other expected indices) is not automatically created on the collection, even though the ttl property is defined and the collection is correctly populated.
Environment
- Spring AI version: 1.1.0‑RC1
- Java version: 17
- Database: MongoDB v7.0
- Schema/driver: Spring Data MongoDB + MongoTemplate
Relevant configuration:
spring:
ai:
chat:
memory:
repository:
mongo:
create-indices: true
ttl: 20
Mongo user permissions: full access (root)
Current result: the collection is created, documents are inserted, but the only index present is id (no TTL index).
Example output of db.collection.getIndexes():
[
{
"v": 2,
"key": { "_id": 1 },
"name": "_id_"
}
]
Steps to reproduce 1. Configure Spring Boot + Spring AI with the configuration above. 2. Start the application; ensure the Mongo chat memory repository is active and messages are being inserted into the collection. 3. Check the indexes on the collection using Mongo shell or a Mongo client (getIndexes()). 4. Observe that the TTL index (with expireAfterSeconds: 20) and other expected indices are not created.
Expected behavior When create-indices: true is set and ttl is defined, Spring AI should automatically create: 1. a TTL index (expireAfterSeconds = ttl) on the document creation date field (or equivalent) 2. any other necessary indices for managing the chat memory (e.g., on conversationId, createdAt, etc.) These indices should be visible in MongoDB via getIndexes().
Minimal Complete Reproducible Example
- Relevant code:
@Configuration
@Slf4j
public class ChatMemoryConfig {
@Autowired
private MongoTemplate mongoTemplate;
@Bean("mongoChatMemoryRepository")
public ChatMemoryRepository mongoChatMemoryRepository(){
return MongoChatMemoryRepository.builder()
.mongoTemplate(mongoTemplate)
.build();
}
@Bean("chatMemoryMongoDB")
public ChatMemory chatMemoryMongoDB(@Qualifier("mongoChatMemoryRepository") ChatMemoryRepository chatMemoryRepository){
return MessageWindowChatMemory.builder()
.chatMemoryRepository(chatMemoryRepository)
.maxMessages(10)
.build();
}
}
@Configuration
@Slf4j
public class ChatClientConfig {
@Bean("chatSyncStreamableClient")
public ChatClient chatSyncStreamableClient(
@Qualifier("llamaMaverickAgentModel") OpenAiChatModel model,
@Qualifier("toolCallbackSyncStreamableProvider") ToolCallbackProvider provider,
@Qualifier("chatMemoryMongoDB") ChatMemory chatMemory
) {
MessageChatMemoryAdvisor messageChatMemoryAdvisor = MessageChatMemoryAdvisor.builder(chatMemory).build();
return ChatClient.builder(model)
.defaultToolCallbacks(provider)
.defaultAdvisors(messageChatMemoryAdvisor)
.build();
}
}
- Relevant dependencies:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
<version>${spring-ai.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-client-chat</artifactId>
<version>${spring-ai.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-chat-memory</artifactId>
<version>${spring-ai.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-chat-memory-repository-mongodb</artifactId>
<version>${spring-ai.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-client</artifactId>
<version>${spring-ai.version}</version>
</dependency>
having: spring-ai.version=1.1.0-RC1.
- Relevant configurations:
spring:
ai:
chat:
memory:
repository:
mongo:
create-indices: true
ttl: 20
Reproduction steps: 1. Start the application and send some messages using the chat memory. 2. Open Mongo shell:
use <yourDb>
db.<chatMemoryCollection>.getIndexes()
Observe that only the _id_ index is present; the automatic and TTL indices are missing.