1.When an index becomes multikey
An 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.
Key features
- MongoDB detects multikey-ness from real inserted data, not from a schema declaration
- The very first document with an array in the indexed field flips the index to multikey
- Once an index is marked multikey, it generally stays marked multikey even if later documents are all scalar
- A validator (section 9) can constrain a field's type to prevent this from happening unintentionally
- Multikey status affects the compound-index array restriction and sort behavior covered by this section
Command
// No syntax to set — MongoDB detects multikey status automatically from the data at write timeMultikey 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.