class Mongoid::Validatable::UniquenessValidator

Validates whether or not a field is unique against the documents in the database.

@example Define the uniqueness validator.

class Person
  include Mongoid::Document
  field :title

  validates_uniqueness_of :title
end

It is also possible to limit the uniqueness constraint to a set of records matching certain conditions:

class Person
  include Mongoid::Document
  field :title
  field :active, type: Boolean

  validates_uniqueness_of :title, conditions: -> {where(active: true)}
end