Chance
Chance

Reputation: 11285

How do I validate the uniqueness of a variable on a nested document across the board in Mongoose / MongoDB?

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?

Upvotes: 0

Views: 330

Answers (1)

Chris Shain
Chris Shain

Reputation: 51319

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.

Upvotes: 2

Related Questions