MongoDB composite primary keys (composite _id and unique compound index)
Codemia Knowledge Hub ·
MongoDB has no native composite primary key. Every document has a single _id primary key, but you can get composite-key behavior by making _id an embedded document or a concatenated string, or by adding a unique index across several fields.
How MongoDB primary keys work
MongoDB is a document-oriented NoSQL database that stores data in BSON (Binary JSON), a flexible JSON-like format. Relevant features:
- Document storage: data lives in JSON-like documents that are easy to read.
- Flexible schema: no fixed schema, so the data structure can change without significant downtime.
- High availability and scaling: replication for redundancy, with sharding to increase read/write performance.
- Rich queries: filtering, sorting and aggregation.
- Indexing: indexes for fast lookups.
Each document is identified by a unique primary key, _id, which MongoDB generates automatically by default and which must be unique within the collection.
In relational databases, a composite primary key combines two or more columns to identify a row. MongoDB does not support that at the engine level, so you emulate it.
Option 1: an embedded document as _id
Put the key fields in a sub-document (or an array) and use it as _id:
Option 2: a concatenated string as _id
Join the field values into one string:
Option 3: a unique compound index
Keep the default _id and enforce uniqueness across several fields with a secondary index:
When composite-style keys make sense
- Uniqueness across fields: a business rule says a combination of fields must be unique.
- Query performance: several fields are always queried together.
- Complex models: relationships or structures that need multi-attribute keys.
Trade-offs
- Data redundancy: composite-style keys in embedded documents can duplicate data.
- Index overhead: extra indexes cost storage and memory.
- Query complexity: enforcing uniqueness or querying on several fields makes queries more complex.
Quick reference
| Feature | MongoDB Implementation |
| Primary Key | Unique _id field, default behavior in MongoDB |
| Composite Keys | Not natively supported; emulate with embedded documents or concatenated strings |
| Secondary Indexes | Supported; allows enforcing uniqueness across multiple fields |
| Schema Flexibility | Dynamic schema, no fixed data model |
| Query Language | Rich querying capabilities, supports complex queries using aggregation framework |
| Use Cases for Emulation | Ensuring field combination uniqueness, optimizing query performance, modeling complex data |
| Considerations | Potential for data redundancy, index overhead, and increased query complexity |
Related reading
- MongoDB and joins
- MongoDB C Driver - Ignore fields on binding
- MongoDB Community Kubernetes Operator and Custom Persistent Volumes
- MongoDB connections from AWS Lambda
- MongoDB 'count' is very slow. How do we refine/work around with it?
- mongodb count num of distinct values per field/key
- MongoDB Data directory /data/db not found
- MongoDB exception in initAndListen 20 Attempted to create a lock file on a read-only directory /data/db, terminating
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.