How do I validate the uniqueness of a variable on a nested document across the board in Mongoose / MongoDB?
Problem
I am trying to figure out how to validate the uniqueness of a field on a Nested Document across the board. Say I have the following schema:
var Store = new Schema({
name : String
, employees: [{name: string, email:string}]
});
Would there be anyway to ensure that the person's email was unique across all stores?
Problem courtesy of: Chance
Solution
I believe that you can prevent duplicates in this case using a unique index in MongoDb: http://www.Mongodb.org/display/DOCS/Indexes#Indexes-UniqueIndexes. If you try to insert an employee with an existing email address, you'll get an exception.
Solution courtesy of: Chris Shain
Discussion
View additional discussion.