mongosh appdb --eval 'db.users.createIndex({email: 1}, {unique: true})'

Category: Databases & SQL

mongosh appdb --eval 'db.users.createIndex({email: 1}, {unique: true})'

Create a unique index

Builds an ascending index on email that rejects duplicate values, enforcing uniqueness at the database level. Indexes are created asynchronously; the command returns when the build finishes. Duplicate documents make the build fail.
Looking for more? Search all 7,657 commands — works offline, in English or Spanish, and fixes typos.