| |
read source history
= createIndex() =
----
[[MongoDB Driver Function Reference]]
Creates an index on the collection.
More information can be found in the corresponding entry of the [[https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/index.html|MongoDB Manual]].
[[[@cG
<collection>.createIndex([<name>, ]<keys>[, <options>[, <commitQuorum>]])
]]]
**Arguments**
[[[@w
|@t0@r0@c0 <collection> | | The target collection. |
| <name> | | Index name |
| <keys> | | A BSON document specifying the index keys. |
| <options> | | A BOSN document specifying options. |
| <commitQuorum> | | A string or integer specifying the commit quorum. |
]]]
**Options**
[[[@cw
{ unique: <boolean>, name: <string>, sparse: <boolean>, expireAfterSeconds: <long>, storageEngine: <document> }
<<@f:text>>
|@t0@r0@c0 unique | | Creates a unique index. |
| name| | Provides an index name. |
| sparse | | Index will only reference documents with the specified key. |
| expireAfterSeconds| | Index expiration time. |
| storageEngine | | Storage engine definition. |
<<@/f>>
]]]
**Example**
[[[@cw
db.employees.createIndex("salary_ix", { salary: 1 });
]]]
Creates an index for the //salary// key on the //employees// collection.
----
[[Main|home]] | [[MongoDB Driver Function Reference]] | [[MongoDB Driver]]
|