Unique or Single field indexes are created on one field (column) of Collection (table). To create a Unique or Single field index the syntax is following:
db.collection_name.ensureIndex({field_name:Sorting_order }, {parameters})
Sorting_order could be Ascending (1) or Descending (-1). By putting the sorting_order you are telling MongoDB to create a unique index on the field and store the value in a specific order. Unique Index does not allow duplicate entries in the field (columns).
For example:
To create Unique Index for deptdetails collection (table) we can issue following command:
db.deptdetails.ensureIndex({"DeptName":1})
What is MongoDB?
How to get Started with MongoDB
How to create or drop Database in MongoDB?
How to create Collection (Table) in MongoDB?
... More
How to get Started with MongoDB
How to create or drop Database in MongoDB?
How to create Collection (Table) in MongoDB?
... More