Reputation: 255
I am new to MongoDB and I want to add validation of a unique field (product slug) among all the documents of the collection.
If the user adds a new product and slug of that new product already available in MongoDB then it should give me an error. I have experience in MySQL. If I make that field unique it will give me an error. So, How could I implement such kind of functionality using MongoDB validator?
Can anyone help me with this?
Upvotes: 0
Views: 42
Reputation: 12305
You can set a unique index on the field.
Something like
db.products.createIndex({"slug": 1}, { "unique": true})
Upvotes: 1