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.