Skip to content
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

MongoDB\Driver\BulkWriteCommand and related APIs #4610

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jmikola
Copy link
Member

@jmikola jmikola commented Apr 11, 2025

@jmikola jmikola requested review from alcaeus and GromNaN April 11, 2025 02:55
<!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.


$manager = new MongoDB\Driver\Manager;

// This example uses configureFailPoint to simulate a top-level command error
Copy link
Member Author

Choose a reason for hiding this comment

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

This seemed the only reliable way to produce a command failure on demand. Most non-server errors will end up being reported as an InvalidArgumentException (e.g. executing an empty BulkWriteCommand, or executing one twice) based on the libmongoc error domain and absence of a partial write result (per logic in phongo_execute_bulkwritecommand()).

?>
]]>
</programlisting>
&example.outputs.similar;
Copy link
Member Author

Choose a reason for hiding this comment

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

I tried to be mindful to use &example.outputs.similar; instead of &example.outputs; for large dumps with variation due to times (e.g. ObjectIds or server responses), but please double check if I missed anything.

</simplelist>
</refsect1>

<refsect1 role="examples">
Copy link
Member Author

@jmikola jmikola Apr 11, 2025

Choose a reason for hiding this comment

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

This borrows the same examples used in BulkWriteCommand's class documentation. The first example is also repeated in BulkWriteCommand::__construct().

Note: I omitted examples in Server::executeBulkWriteCommand() for consistency with executeBulkWrite(). Also, there would be nothing new to show there beyond an explicit call to Manager::selectServer().

</fieldsynopsis>

<classsynopsisinfo role="comment">&InheritedProperties;</classsynopsisinfo>
<!-- <xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('mongodb-driver-exception-serverexception.synopsis')/descendant::db:fieldsynopsis)" /> -->
Copy link
Member Author

@jmikola jmikola Apr 11, 2025

Choose a reason for hiding this comment

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

For context, these <xi:include> blocks are commented out because the corresponding class has nothing for us to inherit. This is necessary to avoid XPointer evaluation failed warnings when building the docs, and is consistent with what we do in other exception classes.


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

var_dump($result->getDeleteResults()->toPHP());
Copy link
Member Author

Choose a reason for hiding this comment

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

I opted to use toPHP() instead of dumping the result MongoDB\BSON\Document directly to ensure users see that counts are returned as Int64 objects.

See PHPC-2568 for more context here.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant