MongoDB allows us to specify a unique constraint on an index. These constraints  will prevent an  application from inserting documents that have duplicate values for the inserted fields. Suppose we have  a collection named  things in a database stuff. db.stuff.things({"a":"apple"}); A unique index can be created by following command. db.students.ensureIndex({student_id:1, class_id:1}, {unique:1}); The _id index