Reputation: 154
I have a simple Tags Collection in Meteor. Currently in order to ensure that a user cannot create a duplicate Tag document I do this:
var existingTag = Tags.findOne({name: "userInput"})
If existingTag is undefined then I can go ahead and do the insert.
Is there a better/correct way of doing this utilizing meteor mongodb syntax? Cant seem to find any documentation on this.
Thanks.
Upvotes: 1
Views: 904
Reputation: 19544
A good solution is to create Mongo index at the unique field. That way you'll have the uniqueness validation at Mongo level, as well as performance increase for searches on that field.
Meteor currently doesn't support index creation directly, so you need to manually log in to your database and add your index from there. The command for this is:
db.tags.ensureIndex({name: 1}, {unique: true})
Here and here you can find more information.
Upvotes: 1