-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-41956: run a command #129
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
Merged
rustagir
merged 14 commits into
mongodb:php-standardization
from
rustagir:DOCSP-41956-run-command
Sep 27, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
992b952
DOCSP-41956: run a command
rustagir 954c3d2
wip
rustagir 832a457
formatting fix
rustagir b6495a0
JS PR fixes 1
rustagir 2dcacc0
link fix
rustagir ea882c2
style fixes
rustagir 13e1c13
JT tech review 1
rustagir 47d3717
wip
rustagir b5da5ab
JM tech review
rustagir 3e0710d
tree
rustagir b77281b
api links
rustagir b08b7bf
links
rustagir 8918fbf
JM tech review 2
rustagir 5053786
small fixes
rustagir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
require __DIR__ . '/vendor/autoload.php'; | ||
|
||
use MongoDB\Client; | ||
|
||
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your connection URI'); | ||
$client = new MongoDB\Client($uri); | ||
|
||
// start-hello | ||
$database = $client->selectDatabase('myDB'); | ||
$cursor = $database->command(['hello' => 1]); | ||
// end-hello | ||
|
||
// start-readpref | ||
$readPref = new MongoDB\Driver\ReadPreference('primaryPreferred'); | ||
$cursor = $database->command( | ||
['hello' => 1], | ||
['readPreference' => $readPref] | ||
); | ||
// end-readpref | ||
|
||
// start-runcommand | ||
$database = $client->accounts; | ||
$command = ['dbStats' => 1]; | ||
|
||
// dbStats returns a single document | ||
$cursor = $database->command($command); | ||
|
||
// Print the first document in the cursor | ||
echo json_encode($cursor->toArray()[0]), PHP_EOL; | ||
// end-runcommand |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
.. _php-run-command: | ||
|
||
====================== | ||
Run a Database Command | ||
====================== | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: administration, code example | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to use the {+php-library+} | ||
to run a database command. You can use database commands to perform a | ||
variety of administrative and diagnostic tasks, such as fetching server | ||
statistics, initializing a replica set, or running an aggregation pipeline. | ||
|
||
.. important:: Prefer Library Methods to Database Commands | ||
|
||
The library provides wrapper methods for many database commands. | ||
We recommend using these methods instead of executing database | ||
commands when possible. | ||
|
||
To perform administrative tasks, use the :mongosh:`MongoDB Shell </>` | ||
instead of the {+php-library+}. The shell provides helper methods | ||
that might not be available in the library. | ||
|
||
If there are no available helpers in the library or the shell, you | ||
can use the ``db.runCommand()`` shell method or the library's | ||
``MongoDB\Database::command()`` method, which is described in this | ||
guide. | ||
|
||
.. _php-execute-command: | ||
|
||
Execute a Command | ||
----------------- | ||
|
||
To run a database command, you must specify the command and any relevant | ||
parameters in a command document, then pass the command document to the | ||
``MongoDB\Database::command()`` method. Many database commands return | ||
multiple result documents, so the ``command()`` method returns a | ||
:php:`MongoDB\Driver\Cursor <mongodb-driver-cursor>` object that you can | ||
iterate through. | ||
|
||
The following code shows how you can use the ``command()`` | ||
method on a :phpclass:`MongoDB\Database` instance to run the ``hello`` | ||
command, which returns information about the server: | ||
|
||
.. literalinclude:: /includes/write/run-command.php | ||
:language: php | ||
:dedent: | ||
:start-after: start-hello | ||
:end-before: end-hello | ||
|
||
To find a link to a full list of database commands and corresponding | ||
parameters, see the :ref:`Additional Information section | ||
<php-addtl-info-runcommand>`. | ||
|
||
.. note:: Read Preference | ||
|
||
The ``command()`` method does not inherit the read preference you might | ||
have set on your ``Database`` instance elsewhere in your code. By | ||
default, ``command()`` uses the ``primary`` read preference. | ||
|
||
You can set a read preference for command execution by setting one | ||
in the options parameter, as shown in the following code: | ||
|
||
.. literalinclude:: /includes/write/run-command.php | ||
:language: php | ||
:dedent: | ||
:start-after: start-readpref | ||
:end-before: end-readpref | ||
|
||
Learn more about the ``ReadPreference`` class in the | ||
:php:`{+extension-short+} API documentation | ||
<mongodb-driver-readpreference>`. | ||
|
||
To learn more about read preference options, see :manual:`Read | ||
Preference </core/read-preference/>` in the {+mdb-server+} manual. | ||
|
||
.. _php-command-response: | ||
|
||
Response | ||
-------- | ||
|
||
The ``command()`` method returns a ``Cursor`` object that contains | ||
the response from the database for the given command. Each database | ||
command performs a different function, so the response | ||
content can vary. | ||
|
||
For commands that return a single result document, | ||
that result is available as the first and only document in the | ||
cursor. For commands that return multiple result | ||
documents, the library converts the cursor | ||
envelope in the raw command response, which includes the cursor ID and | ||
the first batch of results, into an iterable cursor. | ||
|
||
Before you run a command, learn about the response format of the | ||
command so that your application either iterates through multiple | ||
results or extracts the first and only document in the cursor. See the | ||
:ref:`php-addtl-info-runcommand` section of this guide to find a link to | ||
the full list of database commands. | ||
|
||
The raw command response contains the following fields: | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
:widths: 30 70 | ||
|
||
* - Field | ||
- Description | ||
|
||
* - <command result> | ||
- Fields specific to the database command. For example, | ||
the ``count`` command returns the ``n`` field. | ||
|
||
* - ``ok`` | ||
- Whether the command has succeeded (``1``) or failed (``0``). The | ||
library raises a :php:`CommandException | ||
<mongodb-driver-exception-commandexception.php>` if | ||
the ``ok`` field is ``0``. | ||
|
||
* - ``operationTime`` | ||
- The logical time of the operation. MongoDB uses the | ||
logical time to order operations. To learn more about this | ||
concept, see our blog post about the :website:`Global Logical | ||
Clock </blog/post/transactions-background-part-4-the-global-logical-clock>`. | ||
|
||
* - ``$clusterTime`` | ||
- A document that contains the signed cluster time. Cluster time is a | ||
logical time used for the ordering of operations. | ||
|
||
.. _php-command-example: | ||
|
||
Command Example | ||
--------------- | ||
|
||
The following example uses the ``command()`` method to run | ||
the ``dbStats`` command to retrieve storage statistics for the | ||
``accounts`` database: | ||
|
||
.. literalinclude:: /includes/write/run-command.php | ||
:language: php | ||
:dedent: | ||
:start-after: start-runcommand | ||
:end-before: end-runcommand | ||
|
||
The output of this command includes information about the collections in | ||
the database and describes the amount and size of data stored across | ||
collections: | ||
|
||
.. code-block:: none | ||
|
||
{"db":"accounts","collections":2,"views":0,"objects":5,"avgObjSize":22,"dataSize":110, | ||
"storageSize":40960,"totalFreeStorageSize":0,"numExtents":0,"indexes":2,"indexSize":40960, | ||
"indexFreeStorageSize":0,"fileSize":0,"nsSizeMB":0,"ok":1} | ||
|
||
.. _php-addtl-info-runcommand: | ||
|
||
Additional Information | ||
---------------------- | ||
|
||
For more information about the concepts in this guide, see the following | ||
documentation in the {+mdb-server+} manual: | ||
|
||
- :manual:`db.runCommand() </reference/method/db.runCommand/>` | ||
- :manual:`Database Commands </reference/command/>` | ||
- :manual:`hello Command </reference/command/hello/>` | ||
- :manual:`dbStats Command </reference/command/dbStats/>` | ||
|
||
API Documentation | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
For more information about the ``command()`` method, see the | ||
following {+php-library+} API documentation: | ||
|
||
- :phpmethod:`MongoDB\Database::command()` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.