Articles → MONGODB → Unique Index In Mongodb

Unique Index In Mongodb






Purpose





Syntax


db.[collection.createIndex({field:1},{unique:true})



Example




Picture showing the sample document collection




Picture showing unique index created on the name field



Output




Picture showing the error message when user adds duplicate name in document



Posted By  -  Karan Gupta
 
Posted On  -  Friday, April 26, 2024

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250