MongoDBDatabase-createCollection.txt 2.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
=====================================
MongoDB\\Database::createCollection()
=====================================

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

16
.. phpmethod:: MongoDB\\Database::createCollection()
17 18

   Explicitly creates a collection.
19

20 21 22
   .. code-block:: php

      function createCollection($collectionName, array $options = []): array|object
23 24 25 26 27

   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
28
   :manual:`db.createCollection() </reference/method/db.createCollection>` in
29 30
   the :program:`mongo` shell.

31 32 33 34
   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.
35

36
   This method has the following parameters:
37

38
   .. include:: /includes/apiargs/MongoDBDatabase-method-createCollection-param.rst
39 40 41

   The ``$options`` parameter supports the following options:

42 43
   .. include:: /includes/apiargs/MongoDBDatabase-method-createCollection-option.rst

44 45 46 47 48 49
   Note that not all options are available on all versions of MongoDB. Document
   validation, for example, was added in MongoDB 3.2; similarly, the WiredTiger
   storage engine is available only for MongoDB 3.0 and later. Refer to the
   :manual:`create </reference/command/create>` command reference in the MongoDB
   manual for compatibility considerations.

50 51
Return Values
-------------
52

53 54
An array or object with the result document of the :manual:`create
</reference/command/create>` command.
55

56 57 58 59 60 61 62
Errors/Exceptions
-----------------

.. include:: /includes/extracts/error-unsupportedexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

63 64 65
Example
-------

66
The following example creates a ``users`` collection in the ``test``
67 68 69 70
database with document validation criteria:

.. code-block:: php

71 72
   <?php

73
   $db = (new MongoDB\Client)->test;
74 75 76 77 78 79 80

   $result = $db->createCollection('users', [
       'validator' => [
           'username' => ['$type' => 'string'],
           'email' => ['$regex' => '@mongodb\.com$'],
       ],
   ]);
81

82 83 84 85
   var_dump($result);

The output would then resemble::

86
   object(MongoDB\Model\BSONDocument)#11 (1) {
87 88 89 90 91 92 93
     ["storage":"ArrayObject":private]=>
     array(1) {
       ["ok"]=>
       float(1)
     }
   }

94 95
See Also
--------
96

97 98
- :manual:`create </reference/command/create>` command reference in the MongoDB
  manual
99
- :manual:`db.createCollection() </reference/method/db.createCollection>`