Skip to content

DOCS-2720 createIndexes command #1667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/includes/ref-spec-indexes-commands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ sources:
- ref-toc-command-geospatial.yaml
- ref-toc-command-sharding.yaml
files:
- /reference/command/createIndexes
- /reference/command/dropIndexes
- /reference/command/compact
- /reference/command/reIndex
Expand Down
4 changes: 4 additions & 0 deletions source/includes/ref-toc-command-administration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ name: ":dbcommand:`filemd5`"
file: /reference/command/filemd5
description: "Returns the :term:`md5` hash for files stored using :term:`GridFS`."
---
name: ":dbcommand:`createIndexes`"
file: /reference/command/createIndexes
description: "Builds one or more indexes for a collection."
---
name: ":dbcommand:`dropIndexes`"
file: /reference/command/dropIndexes
description: "Removes indexes from a collection."
Expand Down
25 changes: 25 additions & 0 deletions source/reference/command/createIndexes-field.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
object:
name: createIndexes
type: dbcommand
field:
optional: false
type: field
name: createIndexes
type: string
position: 1
description: |
The collection for which to create indexes.
---
object:
name: createIndexes
type: dbcommand
field:
optional: false
type: field
name: indexes
type: array
position: 2
description: |
Specifies the indexes to create. Each document in the array specifies a
separate index.
...
79 changes: 79 additions & 0 deletions source/reference/command/createIndexes-indexes-field.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
object:
name: createIndexes
type: dbcommand
field:
optional: false
type: field
name: key
type: document
position: 1
description: |
Specifies the index's fields. For each field, specify a key-value pair
in which the key is the name of the field to index and the value is
either the index direction or :doc:`index type </core/index-types>`. If
specifying direction, specify ``1`` for ascending or ``-1`` for
descending.
---
object:
name: createIndexes
type: dbcommand
field:
optional: false
type: field
name: name
type: string
position: 2
description: |
A name that uniquely identifies the index.
---
object:
name: createIndexes
type: dbcommand
field:
optional: true
type: field
name: ns
type: string
position: 3
description: |
The :term:`namespace` (i.e. ``<database>.<collection>``) of the
collection for which to create the index. If you omit ``ns``, MongoDB
generates the namespace.
---
file: /reference/method/db.collection.ensureIndex-options-param.yaml
name: background
position: 4
---
file: /reference/method/db.collection.ensureIndex-options-param.yaml
name: unique
position: 5
---
file: /reference/method/db.collection.ensureIndex-options-param.yaml
name: dropDups
position: 6
---
file: /reference/method/db.collection.ensureIndex-options-param.yaml
name: sparse
position: 7
---
file: /reference/method/db.collection.ensureIndex-options-param.yaml
name: expireAfterSeconds
position: 8
---
file: /reference/method/db.collection.ensureIndex-options-param.yaml
name: v
position: 9
---
file: /reference/method/db.collection.ensureIndex-options-param.yaml
name: weights
position: 10
---
file: /reference/method/db.collection.ensureIndex-options-param.yaml
name: default_language
position: 11
---
file: /reference/method/db.collection.ensureIndex-options-param.yaml
name: language_override
position: 12
...

138 changes: 138 additions & 0 deletions source/reference/command/createIndexes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
=============
createIndexes
=============

.. default-domain:: mongodb

.. dbcommand:: createIndexes

Builds multiple indexes on a collection. MongoDB builds the indexes
serially.

The :dbcommand:`createIndexes` command takes the following form:

.. code-block:: javascript

db.runCommand(
{
createIndexes: <collection>,
indexes: [
{
key: {
<key-value_pair>,
<key-value_pair>,
...
},
name: <index_name>,
<option1>,
<option2>,
...
},
{ ... },
{ ... }
]
}
)

.. warning:: An index name, including the :term:`namespace`, cannot be
longer than the :ref:`Index Name Length <limit-index-name-length>` limit.

The :dbcommand:`createIndexes` command takes the following fields:

.. include:: /reference/command/createIndexes-field.rst

Each document in the ``indexes`` array can take the following fields:

.. include:: /reference/command/createIndexes-indexes-field.rst

Behavior
--------

Non-background indexing operations block all other operations on a
database.

If you create an index with one set of options and then issue
:dbcommand:`createIndexes` with the same index fields but different options,
MongoDB will not change the options nor rebuild the index. To
change index options, you must first :method:`drop
<db.collection.dropIndex()>` the existing index before running
:dbcommand:`createIndexes` with the new options.

MongoDB will not create an index on a collection if the value of the index
field in an existing document exceeds the :limit:`index key size limit
<Index Key>`.

Example
-------

The following command builds two indexes on the ``inventory`` collection of
the ``products`` database:

.. code-block:: javascript

db.getSiblingDB("products").runCommand(
{
createIndexes: "inventory",
indexes: [
{
key: {
item: 1,
manufacturer: 1,
model: 1
},
name: "item_manufacturer_model",
unique: true
},
{
key: {
item: 1,
supplier: 1,
model: 1
},
name: "item_supplier_model",
unique: true
}
]
}
)

When the indexes successfully finish building, MongoDB returns a results
document that includes a status of ``"ok" : 1``.

Output
------

The :dbcommand:`createIndexes` command returns a document that indicates
the success of the operation. The document contains some but not all of
the following fields, depending on outcome:

.. data:: createdCollectionAutomatically

If ``true``, then the collection didn't exist and was created in the
process of creating the index.

.. data:: numIndexesBefore

The number of indexes at the start of the command.

.. data:: numIndexesAfter

The number of indexes at the end of the command.

.. data:: ok

A value of ``1`` indicates the indexes are in place. A value of
``0`` indicates an error.

.. data:: note

This ``note`` is returned if an existing index or indexes already
exist. This indicates that the index was not created or changed.

.. data:: errmsg

Returns information about any errors.

.. data:: code

The error code representing the type of error.
2 changes: 1 addition & 1 deletion source/reference/limits.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Indexes
.. limit:: Index Name Length

The names of indexes, including their namespace (i.e database and
collection name) cannot be longer than 125 characters. The default
collection name), cannot be longer than 125 characters. The default
index name is the concatenation of the field names and index
directions.

Expand Down
3 changes: 2 additions & 1 deletion source/reference/privilege-actions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ Database Management Actions

.. authaction:: createIndex

Provides access to the :method:`db.collection.createIndex()` method.
Provides access to the :method:`db.collection.createIndex()` method
and the :dbcommand:`createIndexes` command.
Apply this action to database or collection resources.

.. authaction:: createRole
Expand Down