MongoDB Best Practices

MongoDB Best Practices

Lists a backend engineer's MongoDB habits with confidence scores: query-driven document modeling, indexes, schema validation, write concerns, pagination and security.

MongoDB taste of a backend engineer who models documents around queries

  • Design each collection around the application's read patterns first, then decide what to embed and what to reference. Confidence: 0.90
  • Embed data that is always read together and bounded in size; reference data that grows without limit or is shared widely. Confidence: 0.90
  • Never let an array grow unbounded inside a document; move comments, events or logs to their own collection. Confidence: 0.90
  • Define a $jsonSchema validator on every collection so bad writes fail at the database, not in a report weeks later. Confidence: 0.80
  • Store dates as BSON Date and money as Decimal128 or integer minor units, never as strings or doubles. Confidence: 0.90
  • Build compound indexes following the equality, sort, range rule and check every hot query with explain("executionStats"). Confidence: 0.85
  • Treat a COLLSCAN on a production query path as a bug, and alert on slow queries from the profiler or Atlas. Confidence: 0.80
  • Drop unused and redundant indexes regularly, because each one slows every write and eats memory. Confidence: 0.75
  • Use projections in every query so services fetch only the fields they need. Confidence: 0.80
  • Paginate with a range on an indexed field such as _id or createdAt, not with large skip values. Confidence: 0.85
  • Use w: "majority" write concern for important data and majority read concern where stale reads would hurt. Confidence: 0.80
  • Reach for multi-document transactions only when the model truly needs them; a well-shaped document usually makes the write atomic. Confidence: 0.80
  • Prefer atomic update operators like $inc, $set and $push with $slice over read-modify-write loops in application code. Confidence: 0.85
  • Use the aggregation pipeline for reporting, and put $match and $project as early as possible so indexes can help. Confidence: 0.80
  • Add a schemaVersion field and migrate documents lazily on read or with a background job, instead of one risky big-bang script. Confidence: 0.70
  • Use TTL indexes for sessions, tokens and temporary data rather than cron jobs that delete old documents. Confidence: 0.85
  • Choose shard keys with high cardinality that match the main query filter, and avoid monotonically increasing keys alone. Confidence: 0.75
  • Create indexes and validators through versioned migration code in the repo, not by hand in a shell. Confidence: 0.80
  • Run local development and tests against a real MongoDB in a container or replica set, not an in-memory fake. Confidence: 0.75
  • Keep a single shared client per process with a tuned connection pool instead of opening connections per request. Confidence: 0.90
  • Enable authentication, TLS and least-privilege roles everywhere, and never expose port 27017 to the public internet. Confidence: 0.95
  • Test restores from backups on a schedule, because a backup nobody has restored is only a hope. Confidence: 0.85

Public domain (CC0 1.0) · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 11,989 prompts

More by @OpenSourceBuffGuy

See all
KPI Dashboard Design prompt image

KPI Dashboard Design

Guides the AI in designing KPI dashboards: picking metrics, layout hierarchy, visualisation choices, real-time monitoring and consistent metric definitions.

agent-skill, kpis, data-visualization, dashboard, business-intelligence, best-practices, for-data-analysts, for-executives, for-managers, saas, observability, financial-analysis, table, long-form, explainer, template, structured-output, operations, product-management, data-analysis, sales, marketing

Ink Body Manifesto prompt image

Ink Body Manifesto

Defines a JSON style spec for photographic figures whose torsos are built from black handwriting, on pale grey ground with a heavy black headline and sparse red marks.

image-prompt, json-prompt, poster, typography, text-rendering, photorealistic, portrait, full-body, minimalist, negative-space, limited-palette, black-and-white, editorial, surreal, placeholders, structured-output, style-reference, vertical-format, widescreen, for-designers, for-artists, long-form, advanced, template

Dataverse Python File Operations prompt image

Dataverse Python File Operations

Guides an AI coding assistant on uploading small and large files to Dataverse with the Python SDK, including chunking, validation and practical examples.

rule-file, best-practices, code-generation, python, api, database, data-engineering, for-developers, code, long-form, backend, dataverse, power-platform

Reviews

Loading reviews…

Discussion

Loading comments…