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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
==============
Create Indexes
==============
To create an index on a field or fields, pass an index specification
document to the ``createIndex()`` method:
.. code-block:: 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:
.. code-block:: js
async function createAscendingIndex(db) {
// Get the users collection
const collection = db.collection('users');
// Create the index
const result = await collection.createIndex({ dateOfBirth : 1 });
console.log(result);
return 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:
.. code-block:: js
async function createDescendingIndex(db) {
// Get the documents collection
const collection = db.collection('users');
// Create the index
const result = await collection.createIndex({ lastName : -1 });
console.log(result);
return 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:
.. code-block:: js
async function createCompoundIndex(db) {
// Get the documents collection
const collection = db.collection('users');
// Create the index
const result = await collection.createIndex({ lastName : -1, dateOfBirth : 1 });
console.log(result);
return result;
};
Create a Text Index
-------------------
MongoDB also provides
:manual:`text </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:
.. code-block:: js
async function createTextIndex(db) {
// Get the documents collection
const collection = db.collection('users');
// Create the index
const result = await collection.createIndex({ comments : "text" });
console.log(result);
return result;
};
Create a Hashed Index
---------------------
To specify a :manual:`hashed </core/index-hashed/>` index key,
use the ``hashed`` method.
This example specifies a hashed index key for the ``timestamp`` field:
.. code-block:: js
async function createHashedIndex(db) {
// Get the documents collection
const collection = db.collection('users');
// Create the index
const result = await collection.createIndex({ timestamp : "hashed" });
console.log(result);
return 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 :manual:`2dsphere </core/2dsphere/>`
index key, use one of the ``geo2dsphere`` methods.
This example specifies a 2dsphere index on the ``location`` field:
.. code-block:: js
async function create2dSphereIndex(db) {
// Get the documents collection
const collection = db.collection('users');
// Create the index
const result = await collection.createIndex({ location : "2dsphere" });
console.log(result);
return result;
};
Create a ``2d`` Index
^^^^^^^^^^^^^^^^^^^^^^^^^
To specify a :manual:`2d </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:
.. code-block:: js
async function create2dIndex(db) {
// Get the documents collection
const collection = db.collection('users');
// Create the index
const result = await collection.createIndex({ points : "2d" });
console.log(result);
return 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
^^^^^^^^^^^^^^^^^^^^^
.. code-block:: js
async function createUniqueIndex(db) {
// Get the documents collection
const collection = db.collection('users');
// Create the index
const result = await collection.createIndex(
{ lastName : -1, dateOfBirth : 1 },
{ unique:true }
);
console.log(result);
return result;
};
Create a Partial Index
^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: js
async function createPartialIndex(db) {
// Get the documents collection
const collection = db.collection('users');
// Create the index
const result = await collection.createIndex(
{ lastName : 1, firstName: 1 },
{ partialFilterExpression: { points: { $gt: 5 } }
});
console.log(result);
return result;
};
For other index options, see :manual:`Index Options </core/index-properties/>` .
createIndexes
-------------
The driver also supports the helper method ``createIndexes``. Unlike ``createIndex``,
``createIndexes`` takes in raw index specifications:
.. code-block:: js
async function createMultipleIndexes(db) {
const collection = db.collection('users');
await collection.createIndexes([
// Simple index on field firstName
{
key: { firstName: 1 }
},
// wildcard index
{
key: { '$**': 1 }
},
// named index on lastName and firstName
{
key: { lastName: 1, firstName: -1 }
name: 'lastNameReverseFirstName'
}
]);
});
You can find more info on raw index specifications :manual:`here </reference/command/createIndexes/>` .
|