Unique Indexes

  • 確保沒有其他documents也有同樣的值(field value)
  • 在同一個documents內保持其唯一性(uniqueness), 但不包含分片(not across shards)
  • single value field 也適用在 array

A unique index ensures that the indexed fields do not store duplicate values; i.e. enforces uniqueness for the indexed fields. By default, MongoDB creates a unique index on the _id field during the creation of a collection.

注意:null也算是重複!

Sparse + Unique

  • Unique enforced when value exists
  • Null values allowed in multiple docs
  • Missing field allowed in multiple docs

Documents - Unique Indexes

results matching ""

    No results matching ""