Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 25, 2025

This PR implements comprehensive MongoDB support for the kv-store-adapter library, adding it as a new backend option alongside the existing Redis, Elasticsearch, Memory, and Disk stores.

Implementation

The MongoDB store is implemented using the motor async MongoDB driver and follows the same patterns as existing store implementations:

from kv_store_adapter.stores.mongodb import MongoStore

# Multiple connection options
store = MongoStore(host="localhost", port=27017, database="kvstore")
store = MongoStore(connection_string="mongodb://localhost:27017/kvstore") 
store = MongoStore(client=existing_motor_client, database="custom_db")

# Supports all KVStoreProtocol operations
await store.put(collection="users", key="123", value={"name": "John"}, ttl=3600)
user = await store.get(collection="users", key="123")

Key Features

  • Native TTL Support: Leverages MongoDB's built-in document expiration using expireAfterSeconds indexes
  • Flexible Connection Management: Supports connection strings, existing AsyncIOMotorClient instances, or individual connection parameters
  • Optimized Performance: Creates compound indexes on collection+key for efficient lookups
  • Collection Isolation: Uses MongoDB's natural document structure to isolate collections
  • Automatic Setup: Creates necessary indexes and validates connectivity during initialization

Architecture

The implementation extends BaseManagedKVStore and stores entries as MongoDB documents with this structure:

{
  "collection": "users",
  "key": "123", 
  "value": {"name": "John"},
  "created_at": ISODate("2024-01-01T10:00:00Z"),
  "expires_at": ISODate("2024-01-01T11:00:00Z"),
  "ttl": 3600
}

Indexes are automatically created for:

  • {collection: 1, key: 1} (unique compound index for efficient lookups)
  • {expires_at: 1} (TTL index with expireAfterSeconds: 0)

Testing

The MongoDB store includes comprehensive tests that extend the existing BaseStoreTests class, ensuring full compatibility with the KV Store Protocol. Tests cover:

  • Basic CRUD operations
  • TTL functionality
  • Multiple connection methods
  • Collection management
  • Error handling

Dependencies

Adds motor>=3.0.0 as an optional dependency, installed via:

pip install kv-store-adapter[mongodb]

Documentation

Updated both README.md and DEVELOPING.md to include MongoDB in the list of supported backends and provide configuration examples for all connection methods.

Original prompt

This section details on the original issue you should resolve

<issue_title>Add mongodb support</issue_title>
<issue_description></issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #12

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits September 25, 2025 03:11
Co-authored-by: strawgate <6384545+strawgate@users.noreply.github.com>
Co-authored-by: strawgate <6384545+strawgate@users.noreply.github.com>
Copilot AI changed the title [WIP] Add mongodb support Add MongoDB support to kv-store-adapter Sep 25, 2025
Copilot AI requested a review from strawgate September 25, 2025 03:20
Copilot finished work on behalf of strawgate September 25, 2025 03:20
@strawgate strawgate closed this Sep 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add mongodb support

2 participants