MongoId 组合唯一性索引
MongoId combined uniqueness index
我有三个模型
class Org
include Mongoid::Document
field :name, type: String
embeds_many :org_groups
end
class OrgGroup
include Mongoid::Document
field :name, type: String
embedded_in :org
has_and_belongs_to_many :humans
end
class Human
include Mongoid::Document
field :name, type: String
end
一个人可以在多个组织中,但只能在一个组织组中。
我需要为组织中的人设置唯一性索引。
我该怎么做?
您可以创建一个将由回调调用的方法。
请参阅 documentation 了解回调。
如果您的条件未得到遵守,您可以简单地通过此方法提出一些问题。
询问您是否需要样品。
如果你需要mongodb中的唯一索引,你可以这样做:
class Person
include Mongoid::Document
field :first_name
field :last_name
index({ first_name: 1, last_name: 1 }, { unique: true })
end
文档在这里:
https://docs.mongodb.com/ecosystem/tutorial/mongoid-indexes/
希望对您有所帮助。
我有三个模型
class Org
include Mongoid::Document
field :name, type: String
embeds_many :org_groups
end
class OrgGroup
include Mongoid::Document
field :name, type: String
embedded_in :org
has_and_belongs_to_many :humans
end
class Human
include Mongoid::Document
field :name, type: String
end
一个人可以在多个组织中,但只能在一个组织组中。
我需要为组织中的人设置唯一性索引。
我该怎么做?
您可以创建一个将由回调调用的方法。
请参阅 documentation 了解回调。
如果您的条件未得到遵守,您可以简单地通过此方法提出一些问题。
询问您是否需要样品。
如果你需要mongodb中的唯一索引,你可以这样做:
class Person
include Mongoid::Document
field :first_name
field :last_name
index({ first_name: 1, last_name: 1 }, { unique: true })
end
文档在这里:
https://docs.mongodb.com/ecosystem/tutorial/mongoid-indexes/
希望对您有所帮助。