Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do Collection Field Unique in Meteor JS?

Tags:

meteor

I need to know about collection field unique in meteor js. I have created one collection as shown below:

  List= new Meteor.Collection("list");

The above collection contains 5 fields they are listed below :

 1. User Name

 2. Email

 3. Name

 4. Qualification

 5. Status

In the above fields i need to do unique are User name & Email.So is there any procedure for unique fields in a collection or do manually checking when ever inserts data to a collection each & every time.Please suggest me what to do for this above problem?

like image 556
Venkat Avatar asked Mar 14 '14 14:03

Venkat


2 Answers

Create unique indices on the server. This way, Mongo checks it for you:

List._ensureIndex({username: 1}, {unique: 1});
List._ensureIndex({email: 1}, {unique: 1});

See how Meteor does it for Meteor.users: https://github.com/meteor/meteor/blob/devel/packages/accounts-base/accounts_server.js#L1136

like image 185
Andrew Mao Avatar answered Oct 04 '22 23:10

Andrew Mao


You can try Collection2 with SimpleSchema:

!: https://github.com/aldeed/meteor-collection2

2: https://github.com/aldeed/meteor-simple-schema

like image 44
juliancwirko Avatar answered Oct 04 '22 23:10

juliancwirko