1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
+++
date = "2015-03-17T15:36:56Z"
title = "Create Indexes"
[menu.main]
parent = "Tutorials"
identifier = "Create Indexes"
weight = 35
pre = "<i class='fa'></i>"
+++
# Create Indexes
To create an index on a field or fields, pass an index specification
document to the `createIndex()` method:
```js
{ <field1>: <type1>, <field2>: <type2> ... }
```
## Create an Ascending Index
For an ascending index type, specify ``1`` for ``<type>``.
The following example creates an ascending index key for the
``dateOfBirth`` field:
```js
function createAscendingIndex(db, callback) {
// Get the users collection
const collection = db.collection('users');
// Create the index
collection.createIndex(
{ dateOfBirth : 1 }, function(err, result) {
console.log(result);
callback(result);
});
};
```
## Create a Descending Index
For an descending index type, specify ``-1`` for ``<type>``.
The following example specifies a descending index key on the
``lastName`` field:
```js
function createDescendingIndex(db, callback) {
// Get the documents collection
const collection = db.collection('users');
// Create the index
collection.createIndex(
{ lastName : -1 }, function(err, result) {
console.log(result);
callback(result);
});
};
```
## Create a Compound Index
To specify a compound index, use the ``compoundIndex`` method.
The following example specifies a compound index key composed of the
``lastName`` field sorted in descending order, followed by the
``dateOfBirth`` field sorted in ascending order:
```js
function createCompoundIndex(db, callback) {
// Get the documents collection
const collection = db.collection('users');
// Create the index
collection.createIndex(
{ lastName : -1, dateOfBirth : 1 }, function(err, result) {
console.log(result);
callback(result);
});
};
```
## Create a Text Index
MongoDB also provides
[text](https://docs.mongodb.org/manual/core/index-text/) indexes to
support text search of string content. Text indexes can include any
field whose value is a string or an array of string elements.
This example specifies a text index key for the ``comments`` field:
```js
{{% create-text-index %}}
```
## Create a Hashed Index
To specify a [hashed](https://docs.mongodb.org/manual/core/index-hashed/) index key,
use the ``hashed`` method.
This example specifies a hashed index key for the ``timestamp`` field:
```js
function createHashedIndex(db, callback) {
// Get the documents collection
const collection = db.collection('users');
// Create the index
collection.createIndex(
{ timestamp : "hashed" }, function(err, result) {
console.log(result);
callback(result);
});
};
```
## Create Geospatial Indexes
There are also helpers for creating the index keys for the various
geospatial indexes supported by mongodb.
### Create a `2dsphere` Index
To specify a [2dsphere](https://docs.mongodb.org/manual/core/2dsphere/)
index key, use one of the ``geo2dsphere`` methods.
This example specifies a 2dsphere index on the ``location`` field:
```js
{{% create-2dsphere-index %}}
```
### Create a `2d` Index
To specify a [2d](https://docs.mongodb.org/manual/core/2d/) index key, use the ``geo2d``
method.
.. important::
A 2d index is for data stored as points on a two-dimensional plane
and is intended for legacy coordinate pairs used in MongoDB 2.2 and
earlier.
This example specifies a 2d index on the ``points`` field:
```js
function create2dIndex(db, callback) {
// Get the documents collection
const collection = db.collection('users');
// Create the index
collection.createIndex(
{ points : "2d" }, function(err, result) {
console.log(result);
callback(result);
});
};
```
## IndexOptions
In addition to the index specification document, `createIndex`
method can take an index options document, such as to create unique
indexes or partial indexes.
### Create a Unique Index
```js
function createUniqueIndex(db, callback) {
// Get the documents collection
const collection = db.collection('users');
// Create the index
collection.createIndex(
{ lastName : -1, dateOfBirth : 1 },
{ unique:true },
function(err, result) {
console.log(result);
callback(result);
});
};
```
### Create a Partial Index
```js
function createPartialIndex(db, callback) {
// Get the documents collection
const collection = db.collection('users');
// Create the index
collection.createIndex(
{ lastName : 1, firstName: 1 },
{ partialFilterExpression: { points: { $gt: 5 } } },
function(err, result) {
console.log(result);
callback(result);
});
};
```
For other index options, see [Index Options](https://docs.mongodb.org/manual/core/index-properties/).
|