Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i create composite unique key in mongoDB

How to create a unique key on multiple columns in a collection in mongodb. like we use this ADD UNIQUE unique_index(user, email, phonenumber); in sql.

like image 654
Syam Danda Avatar asked Oct 31 '25 04:10

Syam Danda


1 Answers

Try something like this

db.members.createIndex( { groupNumber: 1, lastname: 1, firstname: 1 },
{ unique: true } )

Check this link for more information mongodb create unique index

like image 143
Jorge Avatar answered Nov 02 '25 08:11

Jorge