Skip to content

MongoDB\Driver\BulkWriteCommand and related APIs #4610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions language-snippets.ent
Original file line number Diff line number Diff line change
Expand Up @@ -3372,6 +3372,16 @@ local: {
</listitem>
</varlistentry>
'>
<!ENTITY mongodb.parameter.bulkwritecommand '
<varlistentry xmlns="http://docbook.org/ns/docbook">
<term><parameter>bulk</parameter> (<classname>MongoDB\Driver\BulkWriteCommand</classname>)</term>
<listitem>
<para>
The write(s) to execute.
</para>
</listitem>
</varlistentry>
'>
<!ENTITY mongodb.parameter.command '
<varlistentry xmlns="http://docbook.org/ns/docbook">
<term><parameter>command</parameter> (<classname>MongoDB\Driver\Command</classname>)</term>
Expand Down Expand Up @@ -3586,14 +3596,17 @@ local: {
'>
<!ENTITY mongodb.returns.cursor '<para xmlns="http://docbook.org/ns/docbook">Returns <classname>MongoDB\Driver\Cursor</classname> on success.</para>'>
<!ENTITY mongodb.returns.writeresult '<para xmlns="http://docbook.org/ns/docbook">Returns <classname>MongoDB\Driver\WriteResult</classname> on success.</para>'>
<!ENTITY mongodb.returns.bulkwritecommandresult '<para xmlns="http://docbook.org/ns/docbook">Returns <classname>MongoDB\Driver\BulkWriteCommandResult</classname> on success.</para>'>
<!ENTITY mongodb.throws.std '&mongodb.throws.argumentparsing;&mongodb.throws.connection;&mongodb.throws.authentication;'>
<!ENTITY mongodb.throws.session-readwriteconcern '<member xmlns="http://docbook.org/ns/docbook">Throws <classname>MongoDB\Driver\Exception\InvalidArgumentException</classname> if the <literal>"session"</literal> option is used with an associated transaction in combination with a <literal>"readConcern"</literal> or <literal>"writeConcern"</literal> option.</member>'>
<!ENTITY mongodb.throws.session-unacknowledged '<member xmlns="http://docbook.org/ns/docbook">Throws <classname>MongoDB\Driver\Exception\InvalidArgumentException</classname> if the <literal>"session"</literal> option is used in combination with an unacknowledged write concern.</member>'>
<!ENTITY mongodb.throws.bulkwritecommandexception '<member xmlns="http://docbook.org/ns/docbook">Throws <classname>MongoDB\Driver\Exception\BulkWriteCommandException</classname> on any write failure (e.g. command failure, write or write concern error)</member>'>
<!ENTITY mongodb.throws.bulkwriteexception '<member xmlns="http://docbook.org/ns/docbook">Throws <classname>MongoDB\Driver\Exception\BulkWriteException</classname> on any write failure (e.g. write error, failure to apply a write concern)</member>'>
<!ENTITY mongodb.throws.argumentparsing '<member xmlns="http://docbook.org/ns/docbook">Throws <classname>MongoDB\Driver\Exception\InvalidArgumentException</classname> on argument parsing errors.</member>'>
<!ENTITY mongodb.throws.authentication '<member xmlns="http://docbook.org/ns/docbook">Throws <classname>MongoDB\Driver\Exception\AuthenticationException</classname> if authentication is needed and fails.</member>'>
<!ENTITY mongodb.throws.connection '<member xmlns="http://docbook.org/ns/docbook">Throws <classname>MongoDB\Driver\Exception\ConnectionException</classname> if connection to the server fails (for reasons other than authentication).</member>'>
<!ENTITY mongodb.throws.bson.unexpected '<member xmlns="http://docbook.org/ns/docbook">Throws <classname>MongoDB\Driver\Exception\UnexpectedValueException</classname> if the input did not contain exactly one BSON document. Possible reasons include, but are not limited to, invalid BSON, extra data (after reading one BSON document), or an unexpected <link xlink:href="&url.mongodb.libbson;" xmlns:xlink="http://www.w3.org/1999/xlink">libbson</link> error.</member>'>
<!ENTITY mongodb.throws.unacknowledged '<member xmlns="http://docbook.org/ns/docbook">Throws <classname>MongoDB\Driver\Exception\LogicException</classname> if the write was not acknowledged.</member>'>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this snippet may be introduced earlier for PHPC-2144, in which case this PR should be rebased.


<!-- Not used in EN anymore -->
<!ENTITY mongodb.note.queryable-encryption-preview ''>
Expand Down
2 changes: 2 additions & 0 deletions reference/mongodb/exceptions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

&reference.mongodb.mongodb.driver.exception.authenticationexception;
&reference.mongodb.mongodb.driver.exception.bulkwriteexception;
&reference.mongodb.mongodb.driver.exception.bulkwritecommandexception;
&reference.mongodb.mongodb.driver.exception.commandexception;
&reference.mongodb.mongodb.driver.exception.connectionexception;
&reference.mongodb.mongodb.driver.exception.connectiontimeoutexception;
Expand Down Expand Up @@ -51,6 +52,7 @@
<listitem>
<simpara><classname>MongoDB\Driver\Exception\ServerException</classname></simpara>
<itemizedlist>
<listitem><simpara><classname>MongoDB\Driver\Exception\BulkWriteCommandException</classname></simpara></listitem>
<listitem><simpara><classname>MongoDB\Driver\Exception\CommandException</classname></simpara></listitem>
<listitem><simpara><classname>MongoDB\Driver\Exception\ExecutionTimeoutException</classname></simpara></listitem>
<listitem>
Expand Down
2 changes: 2 additions & 0 deletions reference/mongodb/mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
&reference.mongodb.mongodb.driver.command;
&reference.mongodb.mongodb.driver.query;
&reference.mongodb.mongodb.driver.bulkwrite;
&reference.mongodb.mongodb.driver.bulkwritecommand;
&reference.mongodb.mongodb.driver.session;
&reference.mongodb.mongodb.driver.clientencryption;
&reference.mongodb.mongodb.driver.serverapi;
Expand All @@ -29,4 +30,5 @@
&reference.mongodb.mongodb.driver.writeconcernerror;
&reference.mongodb.mongodb.driver.writeerror;
&reference.mongodb.mongodb.driver.writeresult;
&reference.mongodb.mongodb.driver.bulkwritecommandresult;
</part>
202 changes: 202 additions & 0 deletions reference/mongodb/mongodb/driver/bulkwritecommand.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->

<reference xml:id="class.mongodb-driver-bulkwritecommand" role="class" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude">

<title>The MongoDB\Driver\BulkWriteCommand class</title>
<titleabbrev>MongoDB\Driver\BulkWriteCommand</titleabbrev>

<partintro>

<!-- {{{ MongoDB\Driver\BulkWriteCommand intro -->
<section xml:id="mongodb-driver-bulkwritecommand.intro">
&reftitle.intro;
<para>
<classname>MongoDB\Driver\BulkWriteCommand</classname> collects one or more
write operations that should be sent to the server using the
<link xlink:href="&url.mongodb.docs.command;bulkWrite">bulkWrite</link>
command introduced in MongoDB 8.0. After adding any number of insert,
update, and delete operations, the command may be executed via
<methodname>MongoDB\Driver\Manager::executeBulkWriteCommand</methodname>.
</para>
<para>
Unlike <classname>MongoDB\Driver\BulkWrite</classname>, where all write
operations must target the same collection, each write operation within
<classname>MongoDB\Driver\BulkWriteCommand</classname> may target a
different collection.
</para>
<para>
Write operations may either be ordered (default) or unordered. Ordered write
operations are sent to the server, in the order provided, for serial
execution. If a write fails, any remaining operations will be aborted.
Unordered operations are sent to the server in an arbitrary order
where they may be executed in parallel. Any errors that occur are reported
after all operations have been attempted.
</para>
</section>
<!-- }}} -->

<section xml:id="mongodb-driver-bulkwritecommand.synopsis">
&reftitle.classsynopsis;

<!-- {{{ Synopsis -->
<classsynopsis>
<ooclass><classname>MongoDB\Driver\BulkWriteCommand</classname></ooclass>

<!-- {{{ Class synopsis -->
<classsynopsisinfo>
<modifier>final</modifier>
<ooclass>
<classname>MongoDB\Driver\BulkWriteCommand</classname>
</ooclass>

<oointerface>
<interfacename>Countable</interfacename>
</oointerface>
</classsynopsisinfo>
<!-- }}} -->

<classsynopsisinfo role="comment">&Methods;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.mongodb-driver-bulkwritecommand')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[not(@role='procedural')])" />
</classsynopsis>
<!-- }}} -->

</section>

<section xml:id="mongodb-driver-bulkwritecommand.examples">
&reftitle.examples;

<example>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These examples are shared with Manager::executeBulkWriteCommand(), and the first is also repeated for BulkWriteCommand::__construct(). This is notably the only example that demonstrates mixed write operations on different collections.

<title>Mixed write operations</title>
<para>
Mixed write operations (i.e. inserts, updates, and deletes) will be sent
to the server using a single
<link xlink:href="&url.mongodb.docs.command;bulkWrite">bulkWrite</link>
command.
</para>
<programlisting role="php">
<![CDATA[
<?php

$manager = new MongoDB\Driver\Manager;

$bulk = new MongoDB\Driver\BulkWriteCommand;

// Delete documents from both collections
$bulk->deleteMany('db.coll_one', []);
$bulk->deleteMany('db.coll_two', []);

// Insert documents into two collections
$bulk->insertOne('db.coll_one', ['_id' => 1]);
$bulk->insertOne('db.coll_two', ['_id' => 2]);
$bulk->insertOne('db.coll_two', ['_id' => 3]);

// Update a document in "coll_one"
$bulk->updateOne('db.coll_one', ['_id' => 1], ['$set' => ['x' => 1]]);

$result = $manager->executeBulkWriteCommand($bulk);

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

?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Inserted 3 document(s)
Updated 1 document(s)
]]>
</screen>
</example>
<example>
<title>Ordered write operations causing an error</title>
<programlisting role="php">
<![CDATA[
<?php

$manager = new MongoDB\Driver\Manager;

$bulk = new MongoDB\Driver\BulkWriteCommand;

$bulk->deleteMany('db.coll', []);
$bulk->insertOne('db.coll', ['_id' => 1]);
$bulk->insertOne('db.coll', ['_id' => 2]);
$bulk->insertOne('db.coll', ['_id' => 1]);
$bulk->insertOne('db.coll', ['_id' => 3]);

try {
$result = $manager->executeBulkWriteCommand($bulk);
} catch (MongoDB\Driver\Exception\BulkWriteCommandException $e) {
$result = $e->getPartialResult();

var_dump($e->getWriteErrors());
}

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

?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
array(1) {
[3]=>
object(MongoDB\Driver\WriteError)#5 (4) {
["message"]=>
string(78) "E11000 duplicate key error collection: db.coll index: _id_ dup key: { _id: 1 }"
["code"]=>
int(11000)
["index"]=>
int(3)
["info"]=>
object(stdClass)#6 (0) {
}
}
}
Inserted 2 document(s)
]]>
</screen>
</example>
</section>

<section xml:id="mongodb-driver-bulkwritecommand.seealso">
&reftitle.seealso;
<simplelist>
<member><methodname>MongoDB\Driver\Manager::executeBulkWriteCommand</methodname></member>
<member><classname>MongoDB\Driver\BulkWriteCommandResult</classname></member>
<member><classname>MongoDB\Driver\Exception\BulkWriteCommandException</classname></member>
<member><classname>MongoDB\Driver\WriteConcern</classname></member>
<member><classname>MongoDB\Driver\WriteConcernError</classname></member>
<member><classname>MongoDB\Driver\WriteError</classname></member>
</simplelist>
</section>

</partintro>

&reference.mongodb.mongodb.driver.entities.bulkwritecommand;

</reference>

<!-- 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
-->
Loading