=================================
MongoDB\\Collection::insertMany()
=================================

.. default-domain:: mongodb

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

Definition
----------

.. phpmethod:: MongoDB\Collection::insertMany()

   Insert multiple documents.

   .. code-block:: php

      function insertMany(
          array $documents,
          array $options = []
      ): MongoDB\InsertManyResult

Parameters
----------

``$documents`` : array
  The documents to insert into the collection.

``$options`` : array
  An array specifying the desired options.

  .. list-table::
     :header-rows: 1
     :widths: 20 20 80

     * - Name
       - Type
       - Description

     * - bypassDocumentValidation
       - boolean
       - If ``true``, allows the write operation to circumvent document level
         validation. Defaults to ``false``.

     * - codec
       - MongoDB\\Codec\\DocumentCodec
       - .. include:: /includes/extracts/collection-option-codec.rst

         .. versionadded:: 1.17

     * - comment
       - mixed
       - .. include:: /includes/extracts/common-option-comment.rst

         .. include:: /includes/extracts/option-requires-4.4.rst

         .. versionadded:: 1.13

     * - ordered
       - boolean
       - If ``true``: when a single write fails, the operation will stop without
         performing the remaining writes and throw an exception.

         If ``false``: when a single write fails, the operation will continue
         with the remaining writes, if any, and throw an exception.

         The default is ``true``.

     * - 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
-------------

A :phpclass:`MongoDB\InsertManyResult` object, which encapsulates a
:php:`MongoDB\Driver\WriteResult <class.mongodb-driver-writeresult>` object.

Errors/Exceptions
-----------------

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

Behavior
--------

.. include:: /includes/extracts/bulkwriteexception-result.rst
.. include:: /includes/extracts/bulkwriteexception-ordered.rst

Example
-------

.. start-crud-include

The following operation inserts two documents into the ``users`` collection
in the ``test`` database:

.. code-block:: php

   <?php

   $collection = (new MongoDB\Client)->test->users;

   $insertManyResult = $collection->insertMany([
       [
           'username' => 'admin',
           'email' => 'admin@example.com',
           'name' => 'Admin User',
       ],
       [
           'username' => 'test',
           'email' => 'test@example.com',
           'name' => 'Test User',
       ],
   ]);

   printf("Inserted %d document(s)\n", $insertManyResult->getInsertedCount());

   var_dump($insertManyResult->getInsertedIds());

The output would then resemble:

.. code-block:: none

   Inserted 2 document(s)
   array(2) {
     [0]=>
     object(MongoDB\BSON\ObjectId)#11 (1) {
       ["oid"]=>
       string(24) "579a25921f417dd1e5518141"
     }
     [1]=>
     object(MongoDB\BSON\ObjectId)#12 (1) {
       ["oid"]=>
       string(24) "579a25921f417dd1e5518142"
     }
   }

.. end-crud-include

See Also
--------

- :phpmethod:`MongoDB\Collection::insertOne()`
- :phpmethod:`MongoDB\Collection::bulkWrite()`
- :doc:`/tutorial/crud`
- :manual:`insert </reference/command/insert>` command reference in the MongoDB
  manual
