I have a company that contains products:

class Company
  include Mongoid::Document
  include Mongoid::Timestamps

  embeds_many :products

  index({"products.code" => 1}, {sparse: true, unique: true})

end

But when I create products(in the same parent) with the same code, like '123', the index does not check the uniqueness of it and it gets created normally. What is wrong?

有帮助吗?

解决方案

Unique indexes are not enforced in embedded documents. There's an open issue on this.

Also, check this page for some workarounds/approaches to enforce this.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top