MongoDB
Composite Primary Keys
Database Design
NoSQL
Data Modeling

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:

json
1{
2  "_id": {
3    "firstField": "value1",
4    "secondField": "value2"
5  },
6  "otherField": "value3"
7}

Option 2: a concatenated string as _id

Join the field values into one string:

json
1{
2  "_id": "value1_value2",
3  "anotherField": "value3"
4}

Option 3: a unique compound index

Keep the default _id and enforce uniqueness across several fields with a secondary index:

javascript
1db.collection.createIndex(
2  { "field1": 1, "field2": 1 },
3  { unique: true }
4)

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

FeatureMongoDB Implementation
Primary KeyUnique _id field, default behavior in MongoDB
Composite KeysNot natively supported; emulate with embedded documents or concatenated strings
Secondary IndexesSupported; allows enforcing uniqueness across multiple fields
Schema FlexibilityDynamic schema, no fixed data model
Query LanguageRich querying capabilities, supports complex queries using aggregation framework
Use Cases for EmulationEnsuring field combination uniqueness, optimizing query performance, modeling complex data
ConsiderationsPotential for data redundancy, index overhead, and increased query complexity

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design