-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathMongoDBCollection-createIndexes.txt
174 lines (124 loc) · 4.8 KB
/
MongoDBCollection-createIndexes.txt
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
====================================
MongoDB\\Collection::createIndexes()
====================================
.. meta::
:description: Create one or more indexes for a collection with the MongoDB PHP Library, with options for unique, compound, and 2dsphere indexes.
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. phpmethod:: MongoDB\Collection::createIndexes()
Create one or more indexes for the collection.
.. code-block:: php
function createIndexes(
array $indexes,
array $options = []
): string[]
Parameters
----------
``$indexes`` : array
The indexes to create on the collection.
For example, the following specifies a unique index on the ``username`` field
and a compound index on the ``email`` and ``createdAt`` fields:
.. code-block:: php
[
[ 'key' => [ 'username' => -1 ], 'unique' => true ],
[ 'key' => [ 'email' => 1, 'createdAt' => 1 ] ],
]
``$options`` : array
An array specifying the desired options.
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Name
- Type
- Description
* - comment
- mixed
- .. include:: /includes/extracts/common-option-comment.rst
.. include:: /includes/extracts/option-requires-4.4.rst
.. versionadded:: 1.13
* - commitQuorum
- string|integer
- Specifies how many data-bearing members of a replica set, including the
primary, must complete the index builds successfully before the primary
marks the indexes as ready.
This option accepts the same values for the ``w`` field in a write
concern plus ``"votingMembers"``, which indicates all voting
data-bearing nodes.
This is not supported for server versions prior to 4.4 and will result
in an exception at execution time if used.
.. versionadded:: 1.7
* - maxTimeMS
- integer
- .. include:: /includes/extracts/common-option-maxTimeMS.rst
.. versionadded:: 1.3
* - session
- :php:`MongoDB\Driver\Session <class.mongodb-driver-session>`
- .. include:: /includes/extracts/common-option-session.rst
.. versionadded:: 1.3
* - writeConcern
- :php:`MongoDB\Driver\WriteConcern <class.mongodb-driver-writeconcern>`
- .. include:: /includes/extracts/collection-option-writeConcern.rst
.. include:: /includes/extracts/common-option-writeConcern-transaction.rst
Return Values
-------------
The names of the created indexes as an array of strings.
Errors/Exceptions
-----------------
.. include:: /includes/extracts/error-unsupportedexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst
Behavior
--------
``$indexes`` parameter
~~~~~~~~~~~~~~~~~~~~~~
The ``$indexes`` parameter is an array of index specification documents. Each
element in ``$indexes`` must itself be an array or object with a ``key`` field,
which corresponds to the ``$key`` parameter of :phpmethod:`createIndex()
<MongoDB\Collection::createIndex()>`. The array or object may include other
fields that correspond to index options accepted by :phpmethod:`createIndex()
<MongoDB\Collection::createIndex()>`. For a full list of the supported index
creation options, refer to the
:manual:`createIndexes </reference/command/createIndexes>` command reference
in the MongoDB manual.
For example, the following ``$indexes`` parameter creates two indexes. The first
is an ascending unique index on the ``username`` field and the second is a
2dsphere index on the ``loc`` field with a custom name:
.. code-block:: none
[
[ 'key' => [ 'username' => 1 ], 'unique' => true ],
[ 'key' => [ 'loc' => '2dsphere' ], 'name' => 'geo_index' ],
]
Example
-------
The following example creates two indexes on the ``restaurants`` collection in
the ``test`` database. One index is a compound index on the ``borough`` and
``cuisine`` fields and the other is 2dsphere index on the ``loc`` field with a
custom name.
.. code-block:: php
<?php
$collection = (new MongoDB\Client)->getCollection('test', 'restaurants');
$indexNames = $collection->createIndexes([
[ 'key' => [ 'borough' => 1, 'cuisine' => 1] ],
[ 'key' => [ 'loc' => '2dsphere'], 'name' => 'geo_index' ],
]);
var_dump($indexNames);
The output would then resemble:
.. code-block:: none
array(2) {
[0]=>
string(19) "borough_1_cuisine_1"
[1]=>
string(9) "geo_index"
}
See Also
--------
- :phpmethod:`MongoDB\Collection::createIndex()`
- :ref:`php-indexes`
- :manual:`createIndexes </reference/command/createIndexes>` command reference
in the MongoDB manual
- :manual:`Index </indexes>` documentation in the MongoDB Manual