agentleFS
ai-enhanced-engineerai-enhanced-engineer/aiee-skills/skills/mongodb-atlas-patterns/SKILL.md

mongodb-atlas-patterns

Skill4 starsChanged 5 months agoMITView on GitHub

MongoDB Atlas + PyMongo 4.8 production patterns — connection pooling, Atlas Search index design (text/autocomplete/fuzzy), aggregation pipelines with $search/$facet, collection caching with TTL, read/write concerns, sync-PyMongo-in-async bridging. Use for schema-less catalog services, full-text/fuzzy matching, and FastAPI services hitting MongoDB Atlas.

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    If any code in this project (or sibling services) uses Motor (motor.motor_asyncio.AsyncIOMotorClient), plan migration to AsyncMongoClient …

    line 88

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

MongoDB Atlas + PyMongo Patterns

PyMongo 4.8 driver patterns specialized for MongoDB Atlas: connection pool sizing for Atlas tiers, Atlas Search index design for fuzzy matching, aggregation pipelines, and the right way to call sync PyMongo from async FastAPI.

When to use

  • Designing a catalog or product-matching service on MongoDB Atlas
  • Tuning Atlas Search indexes for full-text or fuzzy product/SKU lookup
  • Writing aggregation pipelines with $search, $searchMeta, or $facet
  • Bridging sync PyMongo into async FastAPI endpoints (asyncio.to_thread)
  • Evaluating a future migration to AsyncMongoClient (Motor is deprecated 2026-05-14)

For modern Python idioms and async fundamentals, see arch-python-modern. For wrapping sync calls in async endpoints, see fastapi-patterns.

Core patterns

PatternOne-line rule
Pool sizingmaxPoolSize is per replica set member — Atlas M10+ allows 1500 total; M0 caps at 500
Singleton clientMongoClient is thread-safe and pool-managed; one instance per process (module-level or app.state) shares connections across requests. Per-request construction exhausts the Atlas connection cap quickly.
Atlas SRV URImongodb+srv:// implicitly enables TLS; an explicit tls=True is redundant and can conflict with Atlas defaults.
Atlas Search first$search/$searchMeta is only valid as the first stage of an aggregation pipeline — placing it later raises MongoServerError
Search indexDual-mapping string + autocomplete on the same field for full-text + prefix queries
Fuzzy paramsmaxEdits: 1 for ≤5 chars, 2 for longer; prefixLength: 2–3; maxExpansions: 50
Sync-in-asyncCalling sync PyMongo directly inside async def blocks the event loop; await asyncio.to_thread(coll.find_one, q) offloads to the thread pool
Read concernAtlas default local; use majority only for audit/financial reads
Write concernAtlas default w="majority". w=1 returns once the primary acks before replication — fits ephemeral writes (logs, metrics) more than data that must survive a failover
Project early$limit + $project early in pipeline — bound work, drop fields you don't need

Quick reference

# Singleton client (module-level)
mongo_client: MongoClient | None = None

def create_mongo_client():
    global mongo_client
    mongo_client = MongoClient(
        os.environ["MONGODB_URI"],         # mongodb+srv://...
        maxPoolSize=50,
        minPoolSize=5,                     # keep persistent connections warm
        maxIdleTimeMS=30_000,
        serverSelectionTimeoutMS=30_000,   # ≥30s for Atlas failover
        socketTimeoutMS=30_000,
        waitQueueTimeoutMS=5_000,
        retryWrites=True,                  # Atlas defaults
        retryReads=True,
    )

# Atlas Search aggregation (sync PyMongo in async endpoint)
async def search_products(query: str, limit: int = 20) -> list[dict]:
    pipeline = [
        {"$search": {                       # MUST be first stage
            "index": "atlas-search-index",
            "compound": {
                "must": [{"text": {
                    "query": query,
                    "path": ["product_name", "description"],
                    "fuzzy": {"maxEdits": 1, "prefixLength": 2}
                }}],
                "filter": [{"equals": {"path": "status", "value": "active"}}],
            }
        }},
        {"$limit": limit},                  # bound work early
        {"$addFields": {"score": {"$meta": "searchScore"}}},
        {"$project": {"_id": 1, "product_name": 1, "sku": 1, "score": 1}},
    ]
    coll = get_database()["products"]
    return await asyncio.to_thread(lambda: list(coll.aggregate(pipeline)))

Anti-patterns (top 5)

  1. MongoClient(uri) per request → singleton; create once in lifespan
  2. Unbounded .find()/.aggregate() → always $limit early in the pipeline
  3. Missing index on filter field → create_index([("field", ASCENDING)]) or Atlas UI; verify with .explain("executionStats")
  4. Sync PyMongo in async def without thread wrap → await asyncio.to_thread(coll.find_one, q)
  5. $search not first stage → MongoServerError; pre-filters go inside compound.filter, not as preceding $match

Important: Motor deprecated 2026-05-14

If any code in this project (or sibling services) uses Motor (motor.motor_asyncio.AsyncIOMotorClient), plan migration to AsyncMongoClient (PyMongo 4.9+) before May 14, 2026. The current example-service uses sync PyMongo — no Motor migration needed, but evaluate AsyncMongoClient at the next major version bump.

See reference.md for full pattern catalogue, examples.md for project-grounded implementations.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.