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