Skip to content

Add MongoDB-backed ChatMemoryStore implementation#611

Draft
VasilijeJukic01 wants to merge 1 commit intolangchain4j:mainfrom
VasilijeJukic01:feature/mongodb-chat-memory
Draft

Add MongoDB-backed ChatMemoryStore implementation#611
VasilijeJukic01 wants to merge 1 commit intolangchain4j:mainfrom
VasilijeJukic01:feature/mongodb-chat-memory

Conversation

@VasilijeJukic01
Copy link
Copy Markdown

Issue

Closes langchain4j/langchain4j#4830

Change

Ported from the main repository as requested by @dliubarskyi.

This PR introduces MongoDbChatMemoryStore, providing a native MongoDB-backed implementation for chat history persistence. This allows users already utilizing MongoDB in their architecture to store chat memory without needing to introduce a relational database.

Architectural & Design Decisions:

  • New Module (langchain4j-community-mongodb): Created a new module inside the chat-memory-stores category. The ChatMemoryStore relies on standard MongoDB collections and works with any MongoDB deployment (local, self-hosted, etc.). Keeping it separate ensures users don't have to pull in heavy vector search dependencies if they just want standard MongoDB chat storage.
  • Serialization: Messages are serialized to JSON strings using the core ChatMessageSerializer rather than custom BSON arrays. This guarantees out-of-the-box compatibility with all current and future ChatMessage types (e.g., Tool execution, System messages) without needing to maintain complex BSON codecs.
  • TTL Support: Added native support for automatic message expiration via MongoDB TTL indexes (expireAfterSeconds). It includes graceful error handling for MongoCommandException (Error 85) if a user updates the TTL value on an existing index.

Testing:

  • Added MongoDbChatMemoryStoreIT using Testcontainers (mongo:6.0).
  • Tests cover positive CRUD operations, negative validation cases, and memoryId isolation validation.

General checklist

  • There are no breaking changes
  • I have added unit and integration tests for my change
  • I have manually run all the unit tests in all modules, and they are all green
  • I have manually run all integration tests in the module I have added/changed, and they are all green

Checklist for adding new maven module

  • I have added my new module in the root pom.xml and langchain4j-community-bom/pom.xml

Checklist for adding new embedding store integration

  • I have added a {NameOfIntegration}EmbeddingStoreIT that extends from either EmbeddingStoreIT or EmbeddingStoreWithFilteringIT
  • I have added a {NameOfIntegration}EmbeddingStoreRemovalIT that extends from EmbeddingStoreWithRemovalIT

Checklist for changing existing embedding store integration

  • I have manually verified that the {NameOfIntegration}EmbeddingStore works correctly with the data persisted using the latest released version of LangChain4j

@Martin7-1 Martin7-1 added enhancement New feature or request P2 High priority P3 Medium priority theme: memory store Issues/PRs related to memory store labels Mar 31, 2026
@VasilijeJukic01 VasilijeJukic01 force-pushed the feature/mongodb-chat-memory branch from b19d8d8 to 40ed7af Compare March 31, 2026 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request P2 High priority P3 Medium priority theme: memory store Issues/PRs related to memory store

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Add built-in MongoDB-backed ChatMemoryStore implementation

2 participants