|
| 1 | +:orphan: |
| 2 | + |
| 3 | +========================= |
| 4 | +Execute Database Commands |
| 5 | +========================= |
| 6 | + |
| 7 | +.. meta:: |
| 8 | + :description: Learn to execute database commands with the MongoDB PHP Library, including handling single and multiple result documents and setting custom read preferences. |
| 9 | + |
| 10 | + |
| 11 | +.. contents:: On this page |
| 12 | + :local: |
| 13 | + :backlinks: none |
| 14 | + :depth: 2 |
| 15 | + :class: singlecol |
| 16 | + |
| 17 | +Overview |
| 18 | +-------- |
| 19 | + |
| 20 | +The |php-library| provides helper methods across the :phpclass:`Client |
| 21 | +<MongoDB\Client>`, :phpclass:`Database <MongoDB\Database>`, and |
| 22 | +:phpclass:`Collection <MongoDB\Collection>` classes for common |
| 23 | +:manual:`database commands </reference/command>`. In addition, the |
| 24 | +:phpmethod:`MongoDB\Database::command()` method may be used to run database |
| 25 | +commands that do not have a helper method. |
| 26 | + |
| 27 | +The :phpmethod:`MongoDB\Database::command()` method always returns a |
| 28 | +:php:`MongoDB\Driver\Cursor <mongodb-driver-cursor>` object, since it must |
| 29 | +support execution of commands that return single result documents *and* multiple |
| 30 | +results via a command cursor. |
| 31 | + |
| 32 | +Commands That Return a Single Result Document |
| 33 | +--------------------------------------------- |
| 34 | + |
| 35 | +Most database commands return a single result document, which can be obtained by |
| 36 | +converting the returned cursor to an array and accessing its first element. The |
| 37 | +following example executes a :manual:`ping </reference/command/ping>` command |
| 38 | +and prints its result document: |
| 39 | + |
| 40 | +.. code-block:: php |
| 41 | + |
| 42 | + <?php |
| 43 | + |
| 44 | + $database = (new MongoDB\Client)->test; |
| 45 | + |
| 46 | + $cursor = $database->command(['ping' => 1]); |
| 47 | + |
| 48 | + var_dump($cursor->toArray()[0]); |
| 49 | + |
| 50 | +The output would resemble: |
| 51 | + |
| 52 | +.. code-block:: none |
| 53 | + |
| 54 | + object(MongoDB\Model\BSONDocument)#11 (1) { |
| 55 | + ["storage":"ArrayObject":private]=> |
| 56 | + array(1) { |
| 57 | + ["ok"]=> |
| 58 | + float(1) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | +Commands That Yield Multiple Results |
| 63 | +------------------------------------ |
| 64 | + |
| 65 | +Some database commands return a cursor with multiple results. The following |
| 66 | +example executes :manual:`listCollections </reference/command/listCollections>`, |
| 67 | +which returns a cursor containing a result document for each collection in the |
| 68 | +``test`` database, and iterates through the results using a ``foreach`` loop. |
| 69 | +Note that this example is illustrative; applications would generally use |
| 70 | +:phpmethod:`MongoDB\Database::listCollections()` in practice. |
| 71 | + |
| 72 | +.. code-block:: php |
| 73 | + |
| 74 | + <?php |
| 75 | + |
| 76 | + $database = (new MongoDB\Client)->test; |
| 77 | + |
| 78 | + $cursor = $database->command(['listCollections' => 1]); |
| 79 | + |
| 80 | + foreach ($cursor as $collection) { |
| 81 | + echo $collection['name'], "\n"; |
| 82 | + } |
| 83 | + |
| 84 | +The output might resemble the following: |
| 85 | + |
| 86 | +.. code-block:: none |
| 87 | + |
| 88 | + persons |
| 89 | + posts |
| 90 | + zips |
| 91 | + |
| 92 | +.. note:: |
| 93 | + |
| 94 | + At the *protocol* level, commands that yield multiple results via a cursor |
| 95 | + will return a single result document with the essential ingredients for |
| 96 | + constructing the cursor (i.e. the cursor's ID, namespace, and an optional |
| 97 | + first batch of results). If the :php:`MongoDB\Driver\Manager::executeCommand() |
| 98 | + <mongodb-driver-manager.executecommand>` method in the extension detects |
| 99 | + such a response, it will construct an iterable command cursor and return it |
| 100 | + instead of the raw result document. If necessary, raw result documents can |
| 101 | + still be observed using :php:`command monitoring |
| 102 | + <mongodb.tutorial.apm.>`. |
| 103 | + |
| 104 | +Specifying a Custom Read Preference |
| 105 | +----------------------------------- |
| 106 | + |
| 107 | +Write commands, such as :manual:`createUser </reference/command/createUser>`, |
| 108 | +can only be executed on a writable server (e.g. primary replica set |
| 109 | +member). Command helper methods in the |php-library|, such as |
| 110 | +:phpmethod:`MongoDB\Database::drop()`, know to apply their own :term:`read |
| 111 | +preference` if necessary. However, the :phpmethod:`MongoDB\Database::command()` |
| 112 | +method is a generic method and defaults to the read preference of the Database |
| 113 | +object on which it is invoked. When necessary, the ``readPreference`` option may |
| 114 | +be used to override the default read preference. |
| 115 | + |
| 116 | +The following example connects to a cluster and specifies ``secondaryPreferred`` |
| 117 | +as the Client's default read preference. It then specifies a ``primary`` read |
| 118 | +preference when executing the ``createUser`` command on the ``test`` database: |
| 119 | + |
| 120 | +.. code-block:: php |
| 121 | + |
| 122 | + <?php |
| 123 | + |
| 124 | + $client = new MongoDB\Client( |
| 125 | + 'mongodb+srv://cluster0.example.com', |
| 126 | + ['readPreference' => 'secondaryPreferred'] |
| 127 | + ); |
| 128 | + |
| 129 | + $client->test; |
| 130 | + |
| 131 | + $cursor = $db->command( |
| 132 | + [ |
| 133 | + 'createUser' => 'username', |
| 134 | + 'pwd' => 'password', |
| 135 | + 'roles' => ['readWrite'], |
| 136 | + ], |
| 137 | + [ |
| 138 | + 'readPreference' => new MongoDB\Driver\ReadPreference('primary'), |
| 139 | + ] |
| 140 | + ); |
| 141 | + |
| 142 | + var_dump($cursor->toArray()[0]); |
| 143 | + |
| 144 | +The output would then resemble: |
| 145 | + |
| 146 | +.. code-block:: none |
| 147 | + |
| 148 | + object(MongoDB\Model\BSONDocument)#8 (1) { |
| 149 | + ["storage":"ArrayObject":private]=> |
| 150 | + array(1) { |
| 151 | + ["ok"]=> |
| 152 | + float(1) |
| 153 | + } |
| 154 | + } |
0 commit comments