When an index becomes multikey
standardintermediateAn index becomes multikey the moment any document indexed by it stores an array in the indexed field — MongoDB decides this from the actual data at write time, not from a declared schema. It is a property of the index's current data, and it can flip from scalar to multikey the instant one array-valued document is inserted.
Think of it as
Multikey-ness is not a setting you choose when creating the index — it is a fact MongoDB observes and remembers about the index once it sees the first array. This is why a field that has only ever held scalars, on a schema-flexible database, can silently change an index's behavior and restrictions the moment a single document breaks that pattern.
What we're doing: Show an index quietly becoming multikey after an initial run of scalar-only inserts.
- 1
- The index definition itself gives no indication of whether it will ever be multikey — that depends entirely on the data inserted afterward.
- 5
- This single array-valued insert is what flips the index's status — nothing about the createIndex call or the two prior scalar inserts predicted it.
Why this works: Because MongoDB is schema-flexible, "this field is always a scalar" is an assumption about the data, not a guarantee the database enforces on its own — multikey status quietly tracks whatever the data actually turns out to be, which is exactly why a validator is the tool for enforcing the assumption if it needs to hold.
Assuming a field will never hold an array because it never has so far
Wrong
Better
What you see: A compound index that relied on a field staying scalar starts hitting the compound-multikey restriction (or unexpected sort behavior) the first time someone inserts an array there.
Why: Schema flexibility means nothing at the database level prevents a field from becoming an array unless a validator says otherwise — an index's behavior assumptions about a field's shape need the same explicit enforcement as any other structural guarantee.
Remember: Multikey status is detected from real data, not declared — the first document with an array in the indexed field flips it, and it generally stays flipped. Enforce a field's scalar-ness with a validator if the index design depends on it.
See also: multikey indexes · compound multikey restrictions

