Articles → MONGODB → Unique Index In MongodbUnique Index In MongodbIn this article, we will create a unique index in MongoDB.Purpose The unique index ensures that no duplicate value is inserted into the field.Syntax db.[collection.createIndex({field:1},{unique:true})Example Consider the following collection: -On the name field, a new index is created.Output Once the index is created, insert a duplicate value of the name field. The error message appears.Posted By - Karan Gupta Posted On - Friday, April 26, 2024 Query/Feedback Your Email Id Subject Query/Feedback Characters remaining 250
db.[collection.createIndex({field:1},{unique:true})
Query/Feedback