Search code examples
mongodbmongodb-queryaggregation-frameworkmongodb-atlasmongodb-indexes

Can a unique field be a prefix in a compound index?


Does it make any sense at all, since the each value of the prefix will have only one value of any suffix, there is no further sorting that can be done after the prefix?


Solution

  • Yes, a unique field can be the first one listed in an index.

    The index will only be used for queries that include a filter criteria on or sort by that unique field. Such an index could also be useful for creating covered queries that return data from one of the other indexed fields.