============================
MongoDB\\Database::command()
============================

.. default-domain:: mongodb

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

Definition
----------

.. phpmethod:: MongoDB\\Database::command

   Execute a :manual:`command </reference/command>` on the database.
   
   .. code-block:: php
                   
      function command($command, array $options = []): MongoDB\Driver\Cursor

   :phpmethod:`MongoDB\\Database::command` has the following parameters:

   .. include:: /includes/apiargs/MongoDBDatabase-method-command-param.rst
   
   ``command`` supports the following options:
   
   .. include:: /includes/apiargs/MongoDBDatabase-method-command-option.rst

Example
-------

The following operation creates a new user in the database using
the :dbcommand:`createUser` command:

.. code-block:: php

   $database = (new MongoDB\Client)->example;

   $newUser = $database->command(
       [
           'createUser' => 'admin',
           'pwd' => 'admin123',
           'roles' => [
               'readWrite'
           ]
       ]
   );

   

   var_dump($newUser);

The output would resemble::
  
  object(MongoDB\Driver\Cursor)#10 (2) {
     ["cursor"]=>
     array(17) {
       ["stamp"]=>
       int(0)
       ["is_command"]=>
       bool(true)
       ["sent"]=>
       bool(true)
       ["done"]=>
       bool(false)
       ["end_of_event"]=>
       bool(false)
       ["in_exhaust"]=>
       bool(false)
       ["has_fields"]=>
       bool(false)
       ["query"]=>
       object(stdClass)#9 (3) {
         ["createUser"]=>
         string(5) "admin"
         ["pwd"]=>
         string(8) "admin123"
         ["roles"]=>
         array(1) {
           [0]=>
           string(9) "readWrite"
         }
       }
       ["fields"]=>
       object(stdClass)#3 (0) {
       }
       ["read_preference"]=>
       array(2) {
         ["mode"]=>
         int(1)
         ["tags"]=>
         array(0) {
         }
       }
       ["flags"]=>
       int(0)
       ["skip"]=>
       int(0)
       ["limit"]=>
       int(1)
       ["count"]=>
       int(1)
       ["batch_size"]=>
       int(0)
       ["ns"]=>
       string(12) "example.$cmd"
       ["current_doc"]=>
       object(stdClass)#8 (1) {
         ["ok"]=>
         float(1)
       }
     }
     ["server_id"]=>
     int(1)
   }

.. seealso::
   
   - :doc:`/tutorial/commands`
   - :manual:`Database Commands </reference/command>` in the
     MongoDB manual
     
