Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

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.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

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

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×