appa
appa

Reputation: 613

Meteor Collection - How to mark a property as unique

How can I mark a property in a Meteor Collection as unique? I am trying to create a document collection with each document having a name that is unique, and I can't figure out how to make this unique.

Upvotes: 2

Views: 438

Answers (1)

Jim Mack
Jim Mack

Reputation: 1437

You know that mongo assigns a unique, non-human based _id to every document, and that often the title a user decides to call something doesn't need to be unique. That being said,

http://docs.mongodb.org/manual/tutorial/create-a-unique-index/ tells you how to make an index that forces a field to be unique:

db.collection.ensureIndex( { a: 1 }, { unique: true } );

You can run that from the mongo shell. It can also be called from the server js only. If you've created a collection by

Diaries = new Meteor.collection();

then just after adding the collection, in server code, you could add

Diaries.ensureIndex({ title: 1},{ unique: true });

Upvotes: 3

Related Questions