MongoDBDatabase-command.txt 3.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
============================
MongoDB\\Database::command()
============================

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

16
.. phpmethod:: MongoDB\\Database::command()
17 18

   Execute a :manual:`command </reference/command>` on the database.
19

20
   .. code-block:: php
21

22 23
      function command($command, array $options = []): MongoDB\Driver\Cursor

24
   This method has the following parameters:
25 26

   .. include:: /includes/apiargs/MongoDBDatabase-method-command-param.rst
27 28 29

   The ``$options`` parameter supports the following options:

30 31
   .. include:: /includes/apiargs/MongoDBDatabase-method-command-option.rst

32 33
Return Values
-------------
34

35
A :php:`MongoDB\\Driver\\Cursor <class.mongodb-driver-cursor>` object.
36

37 38 39 40 41 42
Errors/Exceptions
-----------------

.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

43 44 45
Example
-------

46 47 48
The following example executes an :manual:`isMaster
</reference/command/isMaster>` command, which returns a cursor with a single
result document:
49 50 51

.. code-block:: php

52
   <?php
53

54
   $database = (new MongoDB\Client)->test;
55

56
   $cursor = $database->command(['isMaster' => 1]);
57

58
   var_dump($c->toArray()[0]);
59 60

The output would resemble::
61 62 63 64 65

   object(MongoDB\Model\BSONDocument)#11 (1) {
     ["storage":"ArrayObject":private]=>
     array(8) {
       ["ismaster"]=>
66
       bool(true)
67 68 69 70 71 72 73 74 75 76
       ["maxBsonObjectSize"]=>
       int(16777216)
       ["maxMessageSizeBytes"]=>
       int(48000000)
       ["maxWriteBatchSize"]=>
       int(1000)
       ["localTime"]=>
       object(MongoDB\BSON\UTCDateTime)#3 (1) {
         ["milliseconds"]=>
         string(13) "1477608046464"
77
       }
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
       ["maxWireVersion"]=>
       int(4)
       ["minWireVersion"]=>
       int(0)
       ["ok"]=>
       float(1)
     }
   }

The following example executes a :manual:`listCollections
</reference/command/listCollections>` command, which returns a cursor with
multiple result documents:

.. code-block:: php

   <?php

95
   $database = (new MongoDB\Client)->test;
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

   $cursor = $database->command(['isMaster' => 1]);

   var_dump($c->toArray());

The output would resemble::

   array(3) {
     [0]=>
     object(MongoDB\Model\BSONDocument)#11 (1) {
       ["storage":"ArrayObject":private]=>
       array(2) {
         ["name"]=>
         string(11) "restaurants"
         ["options"]=>
         object(MongoDB\Model\BSONDocument)#3 (1) {
           ["storage":"ArrayObject":private]=>
           array(0) {
           }
         }
116
       }
117 118 119 120
     }
     [1]=>
     object(MongoDB\Model\BSONDocument)#13 (1) {
       ["storage":"ArrayObject":private]=>
121
       array(2) {
122 123 124 125 126 127 128
         ["name"]=>
         string(5) "users"
         ["options"]=>
         object(MongoDB\Model\BSONDocument)#12 (1) {
           ["storage":"ArrayObject":private]=>
           array(0) {
           }
129 130
         }
       }
131 132 133 134 135 136 137 138 139 140 141 142 143
     }
     [2]=>
     object(MongoDB\Model\BSONDocument)#15 (1) {
       ["storage":"ArrayObject":private]=>
       array(2) {
         ["name"]=>
         string(6) "restos"
         ["options"]=>
         object(MongoDB\Model\BSONDocument)#14 (1) {
           ["storage":"ArrayObject":private]=>
           array(0) {
           }
         }
144 145 146 147
       }
     }
   }

148 149 150 151 152 153 154 155
See Also
--------

- :doc:`/tutorial/commands`
- :manual:`Database Commands </reference/command>` in the MongoDB manual
- :php:`MongoDB\\Driver\\Cursor <class.mongodb-driver-cursor>`
- :php:`MongoDB\\Driver\\Manager::executeCommand()
  <manual/en/mongodb-driver-manager.executecommand.php>`