forked from mongodb/docs-php-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoDBDatabase-createCollection.txt
316 lines (231 loc) · 10.6 KB
/
MongoDBDatabase-createCollection.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
=====================================
MongoDB\\Database::createCollection()
=====================================
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. phpmethod:: MongoDB\Database::createCollection()
Explicitly creates a collection.
.. code-block:: php
function createCollection(
string $collectionName,
array $options = []
): void
MongoDB creates collections implicitly when you first reference the
collection in a command, such as when inserting a document into a new
collection. You may also explicitly create a collection with specific options
using the :phpmethod:`MongoDB\Database::createCollection()` method, or using
:manual:`db.createCollection() </reference/method/db.createCollection>` in
the MongoDB shell.
Explicitly creating collections enables you to create
:manual:`capped collections </core/capped-collections>`, specify
:manual:`document validation criteria </core/document-validation>`,
or configure your storage engine or indexing options.
Parameters
----------
``$collectionName`` : string
The name of the collection to create.
``$options`` : array
An array specifying the desired options.
.. note::
Not all options are available on all versions of MongoDB. Refer to the
:manual:`create </reference/command/create>` command reference in the
MongoDB manual for compatibility considerations.
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Name
- Type
- Description
* - capped
- boolean
- To create a capped collection, specify ``true``. If you specify
``true``, you must also set a maximum size in the ``size`` option.
* - changeStreamPreAndPostImages
- document
- Used to configure support for pre- and post-images in change streams.
See the :manual:`create </reference/command/create>` command
documentation for more information.
.. include:: /includes/extracts/option-requires-6.0.rst
.. versionadded:: 1.13
* - clusteredIndex
- document
- A clustered index specification. See
:manual:`Clustered Collections </core/clustered-collections>` or the
:manual:`create </reference/command/create>` command documentation for
more information.
.. include:: /includes/extracts/option-requires-5.3.rst
.. versionadded:: 1.13
* - collation
- array|object
- .. include:: /includes/extracts/common-option-collation.rst
* - comment
- mixed
- .. include:: /includes/extracts/common-option-comment.rst
.. include:: /includes/extracts/option-requires-4.4.rst
.. versionadded:: 1.13
* - encryptedFields
- document
- A document describing encrypted fields for queryable encryption. If
omitted, the ``encryptedFieldsMap`` option within the
``autoEncryption`` driver option will be consulted. See
`Field Encryption and Queryability <https://www.mongodb.com/docs/manual/core/queryable-encryption/fundamentals/encrypt-and-query/>`_
in the MongoDB manual for more information.
.. include:: /includes/extracts/option-requires-7.0.rst
.. versionadded:: 1.13
* - expireAfterSeconds
- integer
- Used to automatically delete documents in time series collections. See
the :manual:`create </reference/command/create>` command documentation
for more information.
.. include:: /includes/extracts/option-requires-5.0.rst
.. versionadded:: 1.9
* - indexOptionDefaults
- array|object
- Allows users to specify a default configuration for indexes when
creating a collection.
The ``indexOptionDefaults`` option accepts a ``storageEngine``
document, which should take the following form:
.. code-block:: none
{ <storage-engine-name>: <options> }
Storage engine configurations specified when creating indexes are
validated and logged to the :term:`oplog` during replication to support
replica sets with members that use different storage engines.
* - max
- integer
- The maximum number of documents allowed in the capped collection. The
``size`` option takes precedence over this limit. If a capped
collection reaches the ``size`` limit before it reaches the maximum
number of documents, MongoDB removes old documents. If you prefer to
use the ``max`` limit, ensure that the ``size`` limit, which is
required for a capped collection, is sufficient to contain the maximum
number of documents.
* - maxTimeMS
- integer
- .. include:: /includes/extracts/common-option-maxTimeMS.rst
* - pipeline
- array
- An array that consists of the aggregation pipeline stage(s), which will
be applied to the collection or view specified by ``viewOn``. See the
:manual:`create </reference/command/create>` command documentation for
more information.
.. versionadded:: 1.13
* - session
- :php:`MongoDB\Driver\Session <class.mongodb-driver-session>`
- .. include:: /includes/extracts/common-option-session.rst
.. versionadded:: 1.3
* - size
- integer
- Specify a maximum size in bytes for a capped collection. Once a capped
collection reaches its maximum size, MongoDB removes the older
documents to make space for the new documents. The ``size`` option is
required for capped collections and ignored for other collections.
* - storageEngine
- array|object
- Available for the WiredTiger storage engine only.
Allows users to specify configuration to the storage engine on a
per-collection basis when creating a collection. The value of the
``storageEngine`` option should take the following form:
.. code-block:: none
{ <storage-engine-name>: <options> }
Storage engine configurations specified when creating collections are
validated and logged to the :term:`oplog` during replication to support
replica sets with members that use different storage engines.
* - timeseries
- array|object
- An object containing options for creating time series collections. See
the :manual:`create </reference/command/create>` command documentation
for supported options.
.. include:: /includes/extracts/option-requires-5.0.rst
.. versionadded:: 1.9
* - typeMap
- array
- .. include:: /includes/extracts/database-option-typeMap.rst
This will be used for the returned command result document.
* - validationAction
- string
- Determines whether to ``error`` on invalid documents or just ``warn``
about the violations but allow invalid documents to be inserted.
:gold:`IMPORTANT:` Validation of documents only applies to
those documents as determined by the ``validationLevel``.
.. list-table::
:header-rows: 1
* - ``validationAction``
- Description
* - ``"error"``
- **Default**. Documents must pass validation before the write
occurs. Otherwise, the write operation fails.
* - ``"warn"``
- Documents do not have to pass validation. If the document fails
validation, the write operation logs the validation failure.
* - validationLevel
- string
- Determines how strictly MongoDB applies the validation rules to
existing documents during an update.
.. list-table::
:header-rows: 1
* - ``validationLevel``
- Description
* - ``"off"``
- No validation for inserts or updates.
* - ``"strict"``
- **Default**. Apply validation rules to all inserts and all updates.
* - ``"moderate"``
- Apply validation rules to inserts and to updates on existing
*valid* documents. Do not apply rules to updates on existing
*invalid* documents.
* - validator
- array|object
- Allows users to specify :manual:`validation rules or expressions
</core/document-validation>` for the collection. For more information,
see :manual:`Document Validation </core/document-validation>` in the
MongoDB manual.
The ``validator`` option takes an array that specifies the validation
rules or expressions. You can specify the expressions using the same
operators as MongoDB's
:manual:`query operators </reference/operator/query>` with the
exception of :query:`$near`, :query:`$nearSphere`,
:query:`$text`, and :query:`$where`.
Validation occurs during updates and inserts. Existing documents
do not undergo validation checks until modification.
You cannot specify a validator for collections in the ``admin``,
``local``, and ``config`` databases.
You cannot specify a validator for ``system.*`` collections.
* - viewOn
- string
- The name of the source collection or view from which to create the view.
The name is not the full namespace of the collection or view (i.e.
it does not include the database name). Views must be created in the
same databases as the source collection or view.
.. versionadded:: 1.13
* - writeConcern
- :php:`MongoDB\Driver\WriteConcern <class.mongodb-driver-writeconcern>`
- .. include:: /includes/extracts/database-option-writeConcern.rst
Errors/Exceptions
-----------------
.. include:: /includes/extracts/error-unsupportedexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst
Example
-------
The following example creates a ``users`` collection in the ``test``
database with document validation criteria:
.. code-block:: php
<?php
$db = (new MongoDB\Client)->test;
$db->createCollection('users', [
'validator' => [
'username' => ['$type' => 'string'],
'email' => ['$regex' => '@mongodb\.com$'],
],
]);
See Also
--------
- :manual:`create </reference/command/create>` command reference in the MongoDB
manual
- :manual:`db.createCollection() </reference/method/db.createCollection>`
- :manual:`Time Series Collections </core/timeseries-collections>`