<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->

<refentry xml:id="mongodb-driver-bulkwrite.construct" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
 <refnamediv>
  <refname>MongoDB\Driver\BulkWrite::__construct</refname>
  <refpurpose>Create a new BulkWrite</refpurpose>
 </refnamediv>

 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <modifier>public</modifier> <methodname>MongoDB\Driver\BulkWrite::__construct</methodname>
   <methodparam choice="opt"><type class="union"><type>array</type><type>null</type></type><parameter>options</parameter><initializer>&null;</initializer></methodparam>
  </methodsynopsis>
  <para>
   Constructs a new <classname>MongoDB\Driver\BulkWrite</classname>, which is a
   mutable object to which one or more write operations may be added. The
   write(s) may then be executed with
   <methodname>MongoDB\Driver\Manager::executeBulkWrite</methodname>.
  </para>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <variablelist>
   <varlistentry>
    <term><parameter>options</parameter> (<type>array</type>)</term>
    <listitem>
     <para>
      <table>
       <title>options</title>
       <tgroup cols="4">
        <thead>
         <row>
          <entry>Option</entry>
          <entry>Type</entry>
          <entry>Description</entry>
          <entry>Default</entry>
         </row>
        </thead>
        <tbody>
         <row>
          <entry>bypassDocumentValidation</entry>
          <entry><type>bool</type></entry>
          <entry>
           <para>
            If &true;, allows insert and update operations to circumvent
            document level validation.
           </para>
           <para>
            This option is available in MongoDB 3.2+ and is ignored for older
            server versions, which do not support document level validation.
           </para>
          </entry>
          <entry>&false;</entry>
         </row>
         <row>
          <entry>comment</entry>
          <entry><type>mixed</type></entry>
          <entry>
           <para>
            An arbitrary comment to help trace the operation through the
            database profiler, currentOp output, and logs.
           </para>
           <para>
            This option is available in MongoDB 4.4+ and will result in an
            exception at execution time if specified for an older server
            version.
           </para>
          </entry>
         </row>
         &mongodb.option.let;
         <row>
          <entry>ordered</entry>
          <entry><type>bool</type></entry>
          <entry>
           Ordered operations (&true;) are executed serially on the MongoDB
           server, while unordered operations (&false;) are sent to the server
           in an arbitrary order and may be executed in parallel.
          </entry>
          <entry>&true;</entry>
         </row>
        </tbody>
       </tgroup>
      </table>
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>

 <refsect1 role="errors">
  &reftitle.errors;
  <simplelist>
   &mongodb.throws.argumentparsing;
  </simplelist>
 </refsect1>

 <refsect1 role="changelog">
  &reftitle.changelog;
  <para>
   <informaltable>
    <tgroup cols="2">
     <thead>
      <row>
       <entry>&Version;</entry>
       <entry>&Description;</entry>
      </row>
     </thead>
     <tbody>
      <row>
       <entry>PECL mongodb 1.14.0</entry>
       <entry>
        Added the <literal>"comment"</literal> and <literal>"let"</literal>
        options.
       </entry>
      </row>
      <row>
       <entry>PECL mongodb 1.1.0</entry>
       <entry>
        Added the <literal>"bypassDocumentValidation"</literal> option.
       </entry>
      </row>
     </tbody>
    </tgroup>
   </informaltable>
  </para>
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <example>
   <title><function>MongoDB\Driver\BulkWrite::__construct</function> example</title>
   <programlisting role="php">
<![CDATA[
<?php

$bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]);
$bulk->delete([]);
$bulk->insert(['_id' => 1, 'x' => 1]);
$bulk->insert(['_id' => 2, 'x' => 2]);
$bulk->update(
    ['x' => 2],
    ['$set' => ['x' => 1]],
    ['limit' => 1, 'upsert' => false]
);
$bulk->delete(['x' => 1], ['limit' => 1]);
$bulk->update(
    ['_id' => 3],
    ['$set' => ['x' => 3]],
    ['limit' => 1, 'upsert' => true]
);

$manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
$writeConcern = new MongoDB\Driver\WriteConcern(1);

try {
    $result = $manager->executeBulkWrite('db.collection', $bulk, $writeConcern);
} catch (MongoDB\Driver\Exception\BulkWriteException $e) {
    $result = $e->getWriteResult();

    // Check if the write concern could not be fulfilled
    if ($writeConcernError = $result->getWriteConcernError()) {
        printf("%s (%d): %s\n",
            $writeConcernError->getMessage(),
            $writeConcernError->getCode(),
            var_export($writeConcernError->getInfo(), true)
        );
    }

    // Check if any write operations did not complete at all
    foreach ($result->getWriteErrors() as $writeError) {
        printf("Operation#%d: %s (%d)\n",
            $writeError->getIndex(),
            $writeError->getMessage(),
            $writeError->getCode()
        );
    }
} catch (MongoDB\Driver\Exception\Exception $e) {
    printf("Other error: %s\n", $e->getMessage());
    exit;
}

printf("Inserted %d document(s)\n", $result->getInsertedCount());
printf("Updated  %d document(s)\n", $result->getModifiedCount());
printf("Upserted %d document(s)\n", $result->getUpsertedCount());
printf("Deleted  %d document(s)\n", $result->getDeletedCount());

?>
]]>
   </programlisting>
   &example.outputs;
   <screen>
<![CDATA[
Inserted 2 document(s)
Updated  1 document(s)
Upserted 1 document(s)
Deleted  1 document(s)
]]>
   </screen>
  </example>
 </refsect1>


 <refsect1 role="seealso">
  &reftitle.seealso;
  <simplelist>
   <member><methodname>MongoDB\Driver\Manager::executeBulkWrite</methodname></member>
   <member><classname>MongoDB\Driver\WriteResult</classname></member>
  </simplelist>
 </refsect1>

</refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
