MongoDB Naming & Planning Guide: Avoid Common Pitfalls for Databases & Collections
This guide outlines essential MongoDB naming conventions and planning strategies for databases and collections, highlighting common pitfalls, performance impacts of lock contention, and practical case studies on splitting databases and collections to improve scalability.
Starting this series, we will bring you the 360 HULK Cloud Platform MongoDB online practice guide articles. The series shares DBA team’s operational insights, MongoDB usage tips, and solutions to issues encountered in production, aimed at readers with a basic understanding of MongoDB. Official documentation: https://docs.mongodb.com. Chinese community: http://www.mongoing.com
Database
Database names must be all lowercase, may only contain underscores as special characters, and cannot start with a digit (e.g., 123_abc ). Databases are stored as folders; using special characters or non‑standard naming leads to naming chaos.
Database name length is limited to 64 characters.
Before creating a new database, evaluate its size, QPS, etc., and discuss with DBA whether to create a separate database or a dedicated cluster. 【Case 1】A developer, after receiving a MongoDB instance from DBA, created all collections in a single database without consulting DBA because MongoDB’s permission control is relatively loose. Initially the workload was small, but after the business grew and a new high‑write project was launched, batch updates caused frequent database‑level locks, which are exclusive and block reads and writes. Consequently, all database requests became extremely slow. Finally, DBA helped split the database into multiple new databases, converting one‑database‑many‑collections into one‑database‑one‑collection, resolving the performance issue.
Collection
Collection names must be all lowercase, may only contain underscores, cannot start with a digit (e.g., 123_abc ), and must not begin with system . system is the prefix for system collections.
Collection name length is limited to 64 characters.
To avoid database‑level lock problems, large write collections should use a “single database, single collection” structure; therefore, new business should create a new database rather than adding a new collection to an existing one. A large collection in one database can affect read/write performance of other collections.
If a single collection’s data volume is large, split the large table into multiple smaller tables and place each in an independent database. Because MongoDB uses database‑level locks, this greatly reduces lock contention from concurrent writes. 【Case 2】Before launching a business, pressure testing revealed that when update rate exceeded 2500/s, performance dropped sharply due to lock contention. The solution was to hash‑shard the large collection into 16 collections, each placed in a separate database. This reduced lock contention by a factor of 16, and subsequent testing showed update performance reaching 5000/s, surpassing the business requirement of 3000/s.
MongoDB collections support automatic expiration of data via a TTL index on a date field; the field type must be mongoDate() .
This article covered naming and early planning considerations for databases and collections; the next article will discuss document data types, usage tips, and additional online case studies.
360 Zhihui Cloud Developer
360 Zhihui Cloud is an enterprise open service platform that aims to "aggregate data value and empower an intelligent future," leveraging 360's extensive product and technology resources to deliver platform services to customers.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.