Как обеспечить выполнение уникального встроенного документа в монгоиде

У меня есть следующая модель

class Person 
  include Mongoid::Document
  embeds_many :tasks
end

class Task
  include Mongoid::Document
  embedded_in :commit, :inverse_of => :tasks
  field :name
end

Как я могу гарантировать следующее?

person.tasks.create :name => "create facebook killer"
person.tasks.create :name => "create facebook killer"

person.tasks.count == 1

different_person.tasks.create :name => "create facebook killer"
person.tasks.count == 1
different_person.tasks.count == 1

т.е. имена задач уникальны в пределах определенного человека


Проверив документацию по индексам, я подумал, что следующее может сработать:

class Person 
  include Mongoid::Document
  embeds_many :tasks

  index [
      ["tasks.name", Mongo::ASCENDING], 
      ["_id", Mongo::ASCENDING]
  ], :unique => true
end

, но

person.tasks.create :name => "create facebook killer"
person.tasks.create :name => "create facebook killer"

по-прежнему производит дубликат.


Конфигурация индекса, показанная выше в Person, будет преобразована в для mongodb

db.things.ensureIndex({firstname : 1, 'tasks.name' : 1}, {unique : true})
9
задан opsb 2 December 2010 в 11:10
поделиться