Syam Danda
Syam Danda

Reputation: 587

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.

Upvotes: 4

Views: 5849

Answers (1)

Jorge
Jorge

Reputation: 1176

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

Upvotes: 11

Related Questions