|
| 1 | +.. tabs-drivers:: |
| 2 | + |
| 3 | + tabs: |
| 4 | + - id: shell |
| 5 | + content: | |
| 6 | + To create an index in the |
| 7 | + :doc:`Mongo Shell </getting-started/shell/client/>`, use |
| 8 | + :method:`db.collection.createIndex()`. |
| 9 | + |
| 10 | + .. class:: copyable-code |
| 11 | + .. code-block:: javascript |
| 12 | +
|
| 13 | + db.collection.createIndex( <key and index type specification>, <options> ) |
| 14 | +
|
| 15 | + The following example creates a single key descending index on |
| 16 | + the ``name`` field: |
| 17 | + |
| 18 | + .. class:: copyable-code |
| 19 | + .. code-block:: javascript |
| 20 | +
|
| 21 | + db.collection.createIndex( { name: -1 } ) |
| 22 | +
|
| 23 | + The :method:`db.collection.createIndex` method only |
| 24 | + creates an index if an index of the same specification does |
| 25 | + not already exist. |
| 26 | + |
| 27 | + - id: compass |
| 28 | + content: | |
| 29 | + .. important:: |
| 30 | + |
| 31 | + To create an index on a collection in |compass|, |
| 32 | + the collection must contain documents. |
| 33 | + |
| 34 | + To create an index in |
| 35 | + :ref:`MongoDB Compass <compass-index>`, complete the following |
| 36 | + steps: |
| 37 | + |
| 38 | + 1. Navigate to the collection for which you wish to create |
| 39 | + the index. |
| 40 | + |
| 41 | + #. Click the :guilabel:`Indexes` tab. |
| 42 | + |
| 43 | + .. figure:: /images/compass-index-tab.png |
| 44 | + :alt: Compass index tab |
| 45 | + |
| 46 | + .. raw:: html |
| 47 | + |
| 48 | + <br> |
| 49 | + |
| 50 | + #. Click the :guilabel:`Create Index` button. |
| 51 | + |
| 52 | + .. figure:: /images/compass-create-index-button.png |
| 53 | + :alt: Compass index button |
| 54 | + |
| 55 | + .. raw:: html |
| 56 | + |
| 57 | + <br> |
| 58 | + |
| 59 | + The following dialog appears: |
| 60 | + |
| 61 | + .. figure:: /images/compass-index-dialog.png |
| 62 | + :scale: 60 % |
| 63 | + :alt: Compass index dialog |
| 64 | + |
| 65 | + #. (Optional) Enter the index name. |
| 66 | + |
| 67 | + Leaving this field blank causes |compass| to create a |
| 68 | + default name for the index. |
| 69 | + |
| 70 | + #. Add fields to the index. |
| 71 | + |
| 72 | + Use the :guilabel:`Configure the index definition` section |
| 73 | + of the dialog to define the fields for your index and |
| 74 | + their respective types. To create an index on multiple |
| 75 | + fields, click :guilabel:`Add another field`. |
| 76 | + |
| 77 | + #. (Optional) Specify the index options. |
| 78 | + |
| 79 | + The following index options can be specified: |
| 80 | + |
| 81 | + - Build index in the background |
| 82 | + - Create a :ref:`unique index <unique-index>` |
| 83 | + - Create :ref:`TTL <ttl-index>` |
| 84 | + - Partial :ref:`filter expression <partial-index>` |
| 85 | + |
| 86 | + #. Click :guilabel:`Create` to create the index. |
| 87 | + |
| 88 | + - id: python |
| 89 | + content: | |
| 90 | + To create an index using the |
| 91 | + `Python driver <https://api.mongodb.com/python/current/>`_, |
| 92 | + use :py:meth:`pymongo.collection.Collection.create_index`. |
| 93 | + |
| 94 | + .. class:: copyable-code |
| 95 | + .. code-block:: python |
| 96 | +
|
| 97 | + db.collection.create_index([(<key and index type specification>)], <options> ) |
| 98 | +
|
| 99 | + The following example creates a single key descending index on |
| 100 | + the ``name`` field: |
| 101 | + |
| 102 | + .. class:: copyable-code |
| 103 | + .. code-block:: python |
| 104 | +
|
| 105 | + collection.create_index([("name", pymongo.DESCENDING)]) |
| 106 | +
|
| 107 | + The :py:meth:`pymongo.collection.Collection.create_index` |
| 108 | + method only creates an index if an index of the same |
| 109 | + specification does not already exist. |
| 110 | + |
| 111 | + - id: java-sync |
| 112 | + content: | |
| 113 | + To create an index using the |
| 114 | + `Java driver <https://mongodb.github.io/mongo-java-driver/>`_, |
| 115 | + use |
| 116 | + `com.mongodb.client.MongoCollection.createIndex <http://mongodb.github.io/mongo-java-driver/3.4/javadoc/?com/mongodb/client/MongoCollection.html#createIndex-org.bson.conversions.Bson->`_. |
| 117 | + |
| 118 | + .. class:: copyable-code |
| 119 | + .. code-block:: java |
| 120 | +
|
| 121 | + collection.createIndex( <key and index type specification>, <options> ) |
| 122 | +
|
| 123 | + The following example creates a single key descending index on |
| 124 | + the ``name`` field: |
| 125 | + |
| 126 | + .. class:: copyable-code |
| 127 | + .. code-block:: java |
| 128 | +
|
| 129 | + collection.createIndex(Indexes.descending("name")); |
| 130 | +
|
| 131 | + The `com.mongodb.client.MongoCollection.createIndex <http://mongodb.github.io/mongo-java-driver/3.4/javadoc/?com/mongodb/client/MongoCollection.html#createIndex-org.bson.conversions.Bson->`_. |
| 132 | + method only creates an index if an index of the same |
| 133 | + specification does not already exist. |
| 134 | + |
| 135 | + - id: java-async |
| 136 | + content: | |
| 137 | + To create an index using the |
| 138 | + `Async Java driver <http://mongodb.github.io/mongo-java-driver/3.0/driver-async/>`_, |
| 139 | + use |
| 140 | + `com.mongodb.async.client.MongoCollection.createIndex <http://mongodb.github.io/mongo-java-driver/3.4/javadoc/?com/mongodb/async/client/MongoCollection.html#createIndex-org.bson.conversions.Bson-com.mongodb.async.SingleResultCallback->`_. |
| 141 | + |
| 142 | + .. class:: copyable-code |
| 143 | + .. code-block:: java |
| 144 | +
|
| 145 | + collection.createIndex( <key and index type specification>, <options>, <callbackFunction>) |
| 146 | +
|
| 147 | + The following example creates a single key descending index on |
| 148 | + the ``name`` field: |
| 149 | + |
| 150 | + .. class:: copyable-code |
| 151 | + .. code-block:: java |
| 152 | +
|
| 153 | + collection.createIndex(Indexes.descending("name"), someCallbackFunction()); |
| 154 | +
|
| 155 | + The `com.mongodb.async.client.MongoCollection.createIndex <http://mongodb.github.io/mongo-java-driver/3.4/javadoc/?com/mongodb/async/client/MongoCollection.html#createIndex-org.bson.conversions.Bson-com.mongodb.async.SingleResultCallback->`_ |
| 156 | + method only creates an index if an index of the same |
| 157 | + specification does not already exist. |
| 158 | + |
| 159 | + - id: nodejs |
| 160 | + content: | |
| 161 | + To create an index using the |
| 162 | + `Node.JS driver <https://mongodb.github.io/node-mongodb-native/>`_, |
| 163 | + use |
| 164 | + `createIndex() <http://mongodb.github.io/node-mongodb-native/2.1/tutorials/create-indexes/>`_. |
| 165 | + |
| 166 | + .. class:: copyable-code |
| 167 | + .. code-block:: javascript |
| 168 | +
|
| 169 | + collection.createIndex( { <key and index type specification> }, function(err, result) { |
| 170 | + console.log(result); |
| 171 | + callback(result); |
| 172 | + } |
| 173 | +
|
| 174 | + The following example creates a single key descending index on |
| 175 | + the ``name`` field: |
| 176 | +
|
| 177 | + .. class:: copyable-code |
| 178 | + .. code-block:: javascript |
| 179 | +
|
| 180 | + collection.createIndex( { name : -1 }, function(err, result) { |
| 181 | + console.log(result); |
| 182 | + callback(result); |
| 183 | + } |
| 184 | +
|
| 185 | + The `createIndex() <http://mongodb.github.io/node-mongodb-native/2.1/tutorials/create-indexes/>`_ |
| 186 | + method only creates an index if an index of the same |
| 187 | + specification does not already exist. |
| 188 | +
|
| 189 | + - id: php |
| 190 | + content: | |
| 191 | + To create an index using the |
| 192 | + `PHP driver <http://php.net/manual/en/set.mongodb.php>`_, use |
| 193 | + `MongoCollection::createIndex() <http://php.net/manual/en/mongocollection.createindex.php>`_. |
| 194 | +
|
| 195 | + .. class:: copyable-code |
| 196 | + .. code-block:: php |
| 197 | +
|
| 198 | + $collection->createIndex(array(<key and index type specification>), array(<options>)); |
| 199 | +
|
| 200 | + The following example creates a single key descending index on |
| 201 | + the ``name`` field: |
| 202 | +
|
| 203 | + .. class:: copyable-code |
| 204 | + .. code-block:: php |
| 205 | +
|
| 206 | + $collection->createIndex(array('name' => -1)); |
| 207 | +
|
| 208 | + The `MongoCollection::createIndex() <http://php.net/manual/en/mongocollection.createindex.php>`_ |
| 209 | + method only creates an index if an index of the same |
| 210 | + specification does not already exist. |
| 211 | +
|
| 212 | + - id: perl |
| 213 | + content: | |
| 214 | + To create an index using the |
| 215 | + `Perl driver <http://search.cpan.org/dist/MongoDB/lib/MongoDB.pm>`_, |
| 216 | + use |
| 217 | + `create_one() <https://metacpan.org/pod/MongoDB::Examples#CREATE-INDEX-myindexname-ON-users(name)>`_. |
| 218 | +
|
| 219 | + .. class:: copyable-code |
| 220 | + .. code-block:: perl |
| 221 | +
|
| 222 | + my $indexes = $db->get_collection( <collection> )->indexes; |
| 223 | + $indexes->create_one( [ <key and index type specification> ] ); |
| 224 | +
|
| 225 | + The following example creates a single key descending index on |
| 226 | + the ``name`` field: |
| 227 | +
|
| 228 | + .. class:: copyable-code |
| 229 | + .. code-block:: perl |
| 230 | +
|
| 231 | + my $indexes = $db->get_collection( <collection> )->indexes; |
| 232 | + $indexes->create_one( [ name => -1 ] ); |
| 233 | +
|
| 234 | + The `create_one() <https://metacpan.org/pod/MongoDB::Examples#CREATE-INDEX-myindexname-ON-users(name)>`_ |
| 235 | + method only creates an index if an index of the same |
| 236 | + specification does not already exist. |
| 237 | +
|
| 238 | + - id: ruby |
| 239 | + content: | |
| 240 | + To create an index using the |
| 241 | + `Ruby driver <https://api.mongodb.com/ruby/current/>`_, use |
| 242 | + `Mongo::Index::View#create_one <http://www.rubydoc.info/github/mongodb/mongo-ruby-driver/Mongo%2FIndex%2FView%3Acreate_one>`_. |
| 243 | +
|
| 244 | + .. class:: copyable-code |
| 245 | + .. code-block:: ruby |
| 246 | +
|
| 247 | + client[:collection].indexes.create_one({ <key and index type specification> }, {options}) |
| 248 | +
|
| 249 | + The following example creates a single key descending index on |
| 250 | + the ``name`` field: |
| 251 | +
|
| 252 | + .. class:: copyable-code |
| 253 | + .. code-block:: ruby |
| 254 | +
|
| 255 | + client[:collection].indexes.create_one({ name: -1 }) |
| 256 | +
|
| 257 | + The `Mongo::Index::View#create_one <http://www.rubydoc.info/github/mongodb/mongo-ruby-driver/Mongo%2FIndex%2FView%3Acreate_one>`_ |
| 258 | + method only creates an index if an index of the same |
| 259 | + specification does not already exist. |
| 260 | +
|
| 261 | + - id: scala |
| 262 | + content: | |
| 263 | + To create an index using the |
| 264 | + `Scala driver <http://mongodb.github.io/mongo-scala-driver/>`_, |
| 265 | + use |
| 266 | + `org.mongodb.scala.model.Indexes <https://mongodb.github.io/mongo-scala-driver/1.0/scaladoc/index.html#org.mongodb.scala.model.Indexes$>`_. |
| 267 | +
|
| 268 | + .. class:: copyable-code |
| 269 | + .. code-block:: scala |
| 270 | +
|
| 271 | + collection.createIndex(<key and index type specification>) |
| 272 | +
|
| 273 | + The following example creates a single key descending index on |
| 274 | + the ``name`` field: |
| 275 | +
|
| 276 | + .. class:: copyable-code |
| 277 | + .. code-block:: scala |
| 278 | +
|
| 279 | + collection.createIndex(descending("name")) |
| 280 | +
|
| 281 | + The `org.mongodb.scala.model.Indexes <https://mongodb.github.io/mongo-scala-driver/1.0/scaladoc/index.html#org.mongodb.scala.model.Indexes$>`_ |
| 282 | + method only creates an index if an index of the same |
| 283 | + specification does not already exist. |
| 284 | +
|
| 285 | + - id: csharp |
| 286 | + content: | |
| 287 | + To create an index using the |
| 288 | + `.NET driver <http://mongodb.github.io/mongo-csharp-driver/>`_, |
| 289 | + use |
| 290 | + `MongoCollection.CreateIndex <http://api.mongodb.com/csharp/current/html/Overload_MongoDB_Driver_MongoCollection_CreateIndex.htm>`_. |
| 291 | +
|
| 292 | + .. class:: copyable-code |
| 293 | + .. code-block:: csharp |
| 294 | +
|
| 295 | + collection.CreateIndex( IndexKeys<collection>.<key and index type specification>, <options> ); |
| 296 | +
|
| 297 | + The following example creates a single key descending index on |
| 298 | + the ``name`` field: |
| 299 | +
|
| 300 | + .. class:: copyable-code |
| 301 | + .. code-block:: csharp |
| 302 | +
|
| 303 | + collection.CreateIndex( IndexKeys<collection>.Descending("name") ); |
| 304 | +
|
| 305 | + The `MongoCollection.CreateIndex <http://api.mongodb.com/csharp/current/html/Overload_MongoDB_Driver_MongoCollection_CreateIndex.htm>`_ |
| 306 | + method only creates an index if an index of the same |
| 307 | + specification does not already exist. |
0 commit comments