Skip to content

MongoDB

Scope: designing, securing and running MongoDB for applications, self-managed (Community and Enterprise) or on MongoDB Atlas (Atlas Core and Atlas Infinite clusters, and the Free and Flex tiers). The guidelines cover MongoDB 9.0, the current major version, and 8.0 and 7.0, which are supported until October 2029 and August 2027. Architecture-level data rules, such as data ownership, consistency between services and backups, are in general/data.md (DATA-1, DATA-5, DATA-8), and validating input, service identities and TLS in general/security.md (SEC-5, SEC-10, SEC-13). Driver code also follows Node.js or Python, and connecting from serverless functions is covered in Serverless and edge hosting. For a relational system of record, see PostgreSQL; for caching, Redis and caching; and for a document database that clients call directly, Firebase.

Entries are sorted by score from 1 to 10 (criteria): 9–10 start here or strongly recommended, 6–8 recommended, 1–5 further reading or conditional.

Adopted references

  • 9 MongoDB Database Manual (MongoDB docs): the baseline. Read the manual for the version you run; the chapters on data modeling, indexes, transactions, replication, security and production notes matter most for application work.
  • 8 Data Modeling in MongoDB and its best practices (MongoDB docs): the baseline for schema design: modeling for the application's access patterns, embedding or referencing, duplicated data, schema validation and indexes.
  • 8 Atlas Architecture Center (MongoDB docs): the baseline for Atlas: recommended configurations for security, network access, authentication, backups, disaster recovery, high availability and monitoring, organized by the pillars of the Atlas Well-Architected Framework.
  • 8 Security Checklist for Self-Managed Deployments (MongoDB docs): access control, role-based access, TLS, encryption, network exposure, auditing and secure configuration options.
  • 8 Development Checklist (MongoDB docs): data durability, schema design, replication, sharding and driver settings to get right before production.
  • 7 Schema Design Patterns (MongoDB docs): computed values, grouping data, polymorphic data, schema versioning, archiving and the single-collection pattern.
  • 7 Schema Design Anti-Patterns (MongoDB docs): unbounded arrays, too many collections, unnecessary indexes, bloated documents and too many $lookup operations, and how to find them.
  • 7 Production Notes for Self-Managed Deployments (MongoDB docs): platforms, storage, networking, hardware and operating-system settings for running mongod in production.
  • 7 Operations Checklist for Self-Managed Deployments (MongoDB docs): file systems, replication, journaling, hardware, operating-system settings, backups and monitoring.
  • 7 MongoDB Atlas Operational Readiness Checklist (MongoDB docs): a checklist for setting up Atlas organizations, network security, backups, maintenance and monitoring before going live.
  • 7 Indexing Strategies (MongoDB docs): mapping queries to indexes, the ESR guideline, sorting with indexes, selectivity, and unique indexes with schema validation.

Key rules

Data modeling

  • 8 MONGO-1 Design each collection from the application's read and write patterns: embed data that is read or updated together, and reference data that grows without bound, has many items on the child side, is written at different times or exists on its own. MongoDB's core modeling principle is that data accessed together is stored together. A write to one document is atomic, so embedding also removes the need for most multi-document transactions. (MongoDB, Data Modeling, Best Practices: link related data, Best Practices: atomicity)
  • 8 MONGO-2 Keep documents bounded: cap arrays with the subset pattern or move their items to a separate collection, and move fields that aren't read together into separate documents. An unbounded array can exceed the 16 MiB document limit and slows down its indexes, and indexed arrays perform best with fewer than 1,000 elements. Large documents fill the RAM that the working set needs. Store files over the limit with GridFS. (MongoDB, Avoid Unbounded Arrays, Bloated Documents, Limits: BSON documents, Development Checklist: schema design)
  • 7 MONGO-3 When the shape of a document changes, add a schemaVersion field, and make the code's queries and updates handle every version that can still be in the collection. The schema versioning pattern keeps old and new documents side by side and avoids large migrations; a field stored in different places in different versions may need an index for each place. (MongoDB, Maintain Different Schema Versions)
  • 7 MONGO-4 Once a collection's schema is established, add a $jsonSchema validator and keep the default behavior, which rejects invalid inserts and updates. The database then enforces types and allowed values for every client that writes, in addition to validating input at the trust boundary (SEC-10). (MongoDB, Schema Validation, Specify JSON Schema Validation, Best Practices: enforce schema with validation rules)

Indexes and queries

Consistency and transactions

Connections

  • 7 MONGO-10 Create one MongoClient per process when it starts, outside request and serverless handlers, and reuse it for every operation; size maxPoolSize from the number of concurrent requests, starting at 110–115% of the typical number. Each client has its own connection pool, so a client per request or per function invocation opens new connections every time and can exceed the database's connection limits. (MongoDB, Connection Pool Overview, Atlas, Manage Connections with AWS Lambda, Development Checklist: drivers)

Security

Backups and upgrades

How to enforce

Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.

  • 7 Atlas Resource Policies: organization-wide policies, written in Cedar, that Atlas checks on every change from the UI, API, CLI or Terraform. They can forbid /0 entries in IP access lists, require an empty list so that only private endpoints connect, and impose a minimum TLS version. Partly checks MONGO-11, MONGO-13 and SEC-20.
  • 6 CodeQL, Database query built from user-controlled sources and NoSQL Injection (Python): code-scanning queries that trace request data into MongoDB queries in JavaScript, TypeScript and Python. Partly checks MONGO-12.
  • 6 notablescan server parameter: set it on the mongod that integration tests run against, so that every query that needs a collection scan fails. Don't set it in production. Partly checks MONGO-5.
  • 4 Semgrep mongodb-nosqli rule: flags non-constant data passed to $where through the Java driver. Partly checks MONGO-14.

Tutorials and further reading

  • 6 Designing Your Schema (MongoDB docs): the step-by-step schema design process: identify the workload, map relationships and apply design patterns.