-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
db.createCollection.txt
393 lines (241 loc) · 12 KB
/
db.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
=====================
db.createCollection()
=====================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: db.createCollection(name, options)
Creates a new collection explicitly.
Because MongoDB creates a collection implicitly when the collection
is first referenced in a command, this method is used primarily for
creating new collections that use specific options. For example, you use
:method:`db.createCollection()` to create a :term:`capped collection
<capped collection>`, or to create a new collection that uses
:doc:`document validation </core/document-validation>`.
The :method:`db.createCollection()` method has the following prototype form:
.. versionchanged:: 3.2
.. code-block:: javascript
db.createCollection(<name>, { capped: <boolean>,
autoIndexId: <boolean>,
size: <number>,
max: <number>,
storageEngine: <document>,
validator: <document>,
validationLevel: <string>,
validationAction: <string>,
indexOptionDefaults: <document> } )
The :method:`db.createCollection()` method has the following parameters:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - ``name``
- string
- The name of the collection to create.
* - ``options``
- document
- Optional. Configuration options for creating a capped collection or for
preallocating space in a new collection.
The ``options`` document creates a capped collection, preallocates
space in a new ordinary collection, or specifies :doc:`document validation
</core/document-validation>` criteria. The ``options`` document contains
the following fields:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Field
- Type
- Description
* - ``capped``
- boolean
- Optional. To create a :term:`capped collection`,
specify ``true``. If you specify ``true``, you must also set a maximum
size in the ``size`` field.
* - ``autoIndexId``
- boolean
- Optional. Specify ``false`` to disable the automatic creation of an index on the
``_id`` field.
.. important::
For replica sets, do not set ``autoIndexId`` to ``false``.
.. deprecated:: 3.2
The ``autoIndexId`` option will be removed in version 3.4.
* - ``size``
- number
- Optional. 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`` field is
required for capped collections and ignored for other collections.
* - ``max``
- number
- Optional. The maximum number of documents allowed in the capped collection. The
``size`` limit 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.
* - ``usePowerOf2Sizes``
- boolean
- Optional. Available for the MMAPv1 storage engine only.
.. deprecated:: 3.0
For the MMAPv1 storage engine, all collections use the :ref:`power
of 2 sizes allocation <power-of-2-allocation>` unless the
``noPadding`` option is ``true``. The ``usePowerOf2Sizes`` option
does not affect the allocation strategy.
* - ``noPadding``
- boolean
- Optional. Available for the MMAPv1 storage engine only.
.. versionadded:: 3.0
``noPadding`` flag disables the :ref:`power of 2 sizes allocation
<power-of-2-allocation>` for the collection. With ``noPadding``
flag set to true, the allocation strategy does not include
additional space to accommodate document growth, as such, document
growth will result in new allocation. Use for collections with
workloads that are insert-only or in-place updates (such as
incrementing counters).
Defaults to ``false``.
.. warning::
Do not set ``noPadding`` if the workload includes removes or any
updates that may cause documents to grow. For more information,
see :ref:`exact-fit-allocation`.
* - ``storageEngine``
- document
- Optional. Available for the WiredTiger storage engine only.
.. versionadded:: 3.0
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:: javascript
{ <storage-engine-name>: <options> }
Storage engine configuration 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.
* - ``validator``
- document
- Optional. Allows users to specify :doc:`validation rules or expressions
</core/document-validation>` for the collection. For more information,
see :doc:`/core/document-validation`.
.. versionadded:: 3.2
The ``validator`` option takes a document that specifies the
validation rules or expressions. You can specify the expressions using
the same operators as the :ref:`query operators <query-selectors>`
with the exception of :query:`$geoNear`, :query:`$near`,
:query:`$nearSphere`, :query:`$text`, and :query:`$where`.
.. note::
- 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.
* - ``validationLevel``
- string
- Optional. Determines how strictly MongoDB applies the
validation rules to existing documents during an update.
.. versionadded:: 3.2
.. include:: /includes/extracts/table-validationLevel-values.rst
* - ``validationAction``
- string
- Optional. Determines whether to ``error`` on invalid documents or just ``warn``
about the violations but allow invalid documents to be inserted.
.. versionadded:: 3.2
.. important::
Validation of documents only applies to those documents as
determined by the ``validationLevel``.
.. include:: /includes/extracts/table-validationAction-values.rst
* - ``indexOptionDefaults``
- document
- Optional. 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:: javascript
{ <storage-engine-name>: <options> }
Storage engine configuration 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.
.. versionadded:: 3.2
:method:`db.createCollection()` is a wrapper around the database
command :dbcommand:`create`.
Examples
--------
Create a Capped Collection
~~~~~~~~~~~~~~~~~~~~~~~~~~
Capped collections
have maximum size or document counts that prevent them from growing
beyond maximum thresholds. All capped collections must specify a maximum
size and may also specify a maximum document count. MongoDB removes
older documents if a collection reaches the maximum size limit before it
reaches the maximum document count. Consider the following example:
.. code-block:: javascript
db.createCollection("log", { capped : true, size : 5242880, max : 5000 } )
This command creates a collection named ``log`` with a maximum size of 5
megabytes and a maximum of 5000 documents.
See :doc:`/core/capped-collections` for more
information about capped collections.
Create a Collection with Document Validation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. versionadded:: 3.2
Collections with validation compare each inserted or updated document
against the criteria specified in the ``validator`` option. Depending
on the ``validationLevel`` and ``validationAction``, MongoDB either
returns a warning, or refuses to insert or update the document if it
fails to meet the specified criteria.
The following example creates a ``contacts`` collection with a validator
that specifies that inserted or updated documents should match at least
one of three following conditions:
- the ``phone`` field is a string
- the ``email`` field matches the regular expression
- the ``status`` field is either ``Unknown`` or ``Incomplete``.
.. code-block:: javascript
db.createCollection( "contacts",
{
validator: { $or:
[
{ phone: { $type: "string" } },
{ email: { $regex: /@mongodb\.com$/ } },
{ status: { $in: [ "Unknown", "Incomplete" ] } }
]
}
}
)
With the validator in place, the following insert operation fails validation:
.. code-block:: javascript
db.contacts.insert( { name: "Amanda", status: "Updated" } )
The method returns the error in the ``WriteResult``:
.. code-block:: javascript
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 121,
"errmsg" : "Document failed validation"
}
})
For more information, see :doc:`/core/document-validation`. To view the
validation specifications for a collection, use the
:method:`db.getCollectionInfos()` method.
.. _create-collection-storage-engine-options:
Specify Storage Engine Options
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. versionadded:: 3.0
You can specify collection-specific storage engine configuration
options when you create a collection with
:method:`db.createCollection()`. Consider the following operation:
.. code-block:: javascript
db.createCollection(
"users",
{ storageEngine: { wiredTiger: { configString: "<option>=<setting>" } } }
)
This operation creates a new collection named ``users`` with a
specific configuration string that MongoDB will pass to the
``wiredTiger`` storage engine. See the :wtdocs:`WiredTiger documentation of
collection level options </struct_w_t___s_e_s_s_i_o_n.html>`
for specific ``wiredTiger`` options.