Observed since 20 Oct, 18:42 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
actvalue.mongo-client
Singleton MongoDB client for AWS Lambda and serverless environments
Singleton client for MongoDB connection
pip install actvalue.mongo-client
Or with uv:
uv add actvalue.mongo-client
The client is used to create and share MongoDB connection pool.
import asyncio
import os
from mongo_client import MongoDbConnection
# Initialize your connection parameters and optionally set pool size
os.environ['MONGO_URL'] = 'mongodb+srv://<your-connection>/database'
os.environ['MONGO_POOL_SIZE'] = '5' # default value
# Create a singleton instance at module level
mongo = MongoDbConnection()
async def main():
# Create connection pool if not existing already
db = await mongo.get_db()
users = db["users"]
await users.insert_one({"username": "test123", "password": "test123"})
# Some other time, some other place in code
# Connection pool is reused
db = await mongo.get_db()
user = await users.find_one({"username": "test123"})
print(user)
asyncio.run(main())
Perfect for serverless environments where connection reuse is critical:
import os
from mongo_client import MongoDbConnection
# Set environment variables (typically from Lambda configuration)
os.environ['MONGO_URL'] = 'mongodb+srv://...'
os.environ['MONGO_POOL_SIZE'] = '5'
async def lambda_handler(event, context):
# IMPORTANT: Initialize within handler due to Python asyncio event loop limitations
# Each Lambda invocation may create a new event loop, and AsyncMongoClient
# is bound to the event loop it was created on
mongo = MongoDbConnection()
db = await mongo.get_db()
collection = db["my_collection"]
result = await collection.find_one({"_id": event["id"]})
return {"statusCode": 200, "body": result}
⚠️ Python Lambda Note: Unlike the TypeScript version, the Python client should be initialized inside the handler function. This is because Python's AsyncMongoClient binds to the asyncio event loop at creation time. AWS Lambda may create new event loops between invocations, causing "Cannot use AsyncMongoClient in different event loop" errors if the instance is created at module level.
MONGO_URL: MongoDB connection string (required)MONGO_POOL_SIZE: Connection pool size (optional, default: "5")MIT
Download counts and popularity will appear here as the collector receives them.
| Package / target | Requirement | Relationship |
|---|---|---|
pymongoEnvironment{
"extras": "",
"marker": null,
"requires_python": ">=3.13",
"evaluation": "NOT_EVALUATED"
} | pymongo>=4.17.0 | runtime |
mypyEnvironment{
"extras": "",
"marker": "extra == \"dev\"",
"requires_python": ">=3.13",
"evaluation": "NOT_EVALUATED"
} | mypy>=1.19.1; extra == "dev" | runtime |
pytest-asyncioEnvironment{
"extras": "",
"marker": "extra == \"dev\"",
"requires_python": ">=3.13",
"evaluation": "NOT_EVALUATED"
} | pytest-asyncio>=1.4.0; extra == "dev" | runtime |
pytest-covEnvironment{
"extras": "",
"marker": "extra == \"dev\"",
"requires_python": ">=3.13",
"evaluation": "NOT_EVALUATED"
} | pytest-cov>=7.1.0; extra == "dev" | runtime |
pytestEnvironment{
"extras": "",
"marker": "extra == \"dev\"",
"requires_python": ">=3.13",
"evaluation": "NOT_EVALUATED"
} | pytest>=9.1.1; extra == "dev" | runtime |
ruffEnvironment{
"extras": "",
"marker": "extra == \"dev\"",
"requires_python": ">=3.13",
"evaluation": "NOT_EVALUATED"
} | ruff>=0.16.5; extra == "dev" | runtime |
bdist_wheel · 3.97 KB · Python py3
sdist · 62.88 KB · Python source
Observed since 20 Oct, 18:42 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package_version:573ae590cfd74df06f57fca181dce90faa1d454b91c411e48a751ef3f9635162