|
| 1 | +.. _ruby-run-command: |
| 2 | + |
| 3 | +====================== |
| 4 | +Run a Database Command |
| 5 | +====================== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: administration, code example |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use the {+driver-short+} to |
| 24 | +run a database command. You can use database commands to perform a |
| 25 | +variety of administrative and diagnostic tasks, such as fetching server |
| 26 | +statistics, initializing a replica set, or running an aggregation pipeline. |
| 27 | + |
| 28 | +.. important:: Prefer Driver Methods to Database Commands |
| 29 | + |
| 30 | + The driver provides wrapper methods for many database commands. |
| 31 | + If possible, we recommend using these methods instead of executing |
| 32 | + database commands. |
| 33 | + |
| 34 | + To perform administrative tasks, use the :mongosh:`MongoDB Shell </>` |
| 35 | + instead of the {+driver-short+}. The shell provides helper methods |
| 36 | + that might not be available in the driver. |
| 37 | + |
| 38 | + If there are no available helpers in the driver or the shell, you |
| 39 | + can use the ``db.runCommand()`` shell method or the driver's |
| 40 | + ``command`` method, which is described in this guide. |
| 41 | + |
| 42 | +Sample Data |
| 43 | +~~~~~~~~~~~ |
| 44 | + |
| 45 | +The examples in this guide use the ``sample_restaurants`` |
| 46 | +database from the :atlas:`Atlas sample datasets </sample-data>`. To access this database |
| 47 | +from your {+language+} application, create a ``Mongo::Client`` object that connects to an Atlas cluster |
| 48 | +and assign the following value to your ``database`` variable: |
| 49 | + |
| 50 | +.. literalinclude:: /includes/databases-collections/run-command.rb |
| 51 | + :language: scala |
| 52 | + :dedent: |
| 53 | + :start-after: start-db |
| 54 | + :end-before: end-db |
| 55 | + |
| 56 | +To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the |
| 57 | +:atlas:`Get Started with Atlas </getting-started>` guide. |
| 58 | + |
| 59 | +Execute a Command |
| 60 | +----------------- |
| 61 | + |
| 62 | +To run a database command, run the ``command`` instance method of a ``Mongo::Database`` |
| 63 | +instance and pass the name of the operation to run as a parameter. |
| 64 | + |
| 65 | +The following example calls the ``command`` method to run the ``hello`` command, which |
| 66 | +returns information about the server: |
| 67 | + |
| 68 | +.. literalinclude:: /includes/databases-collections/run-command.rb |
| 69 | + :language: scala |
| 70 | + :dedent: |
| 71 | + :start-after: start-hello |
| 72 | + :end-before: end-hello |
| 73 | + |
| 74 | +.. tip:: |
| 75 | + |
| 76 | + To view a full list of database commands and their corresponding |
| 77 | + parameters, see :manual:`Database Commands </reference/command/>` in |
| 78 | + the {+mdb-server+} manual. |
| 79 | + |
| 80 | +Set a Read Preference |
| 81 | +---------------------- |
| 82 | + |
| 83 | +The ``command`` method does not inherit the read preference you might |
| 84 | +have set on your ``Database`` instance. By default, ``command`` |
| 85 | +uses the ``primary`` read preference. |
| 86 | + |
| 87 | +You can set a read preference for the command execution by passing the |
| 88 | +``:read`` opotion to the ``command`` method, as |
| 89 | +shown in the following code: |
| 90 | + |
| 91 | +.. literalinclude:: /includes/databases-collections/run-command.rb |
| 92 | + :language: scala |
| 93 | + :dedent: |
| 94 | + :start-after: start-readpref |
| 95 | + :end-before: end-readpref |
| 96 | + |
| 97 | +.. tip:: |
| 98 | + |
| 99 | + To learn more about read preference options, see :manual:`Read |
| 100 | + Preference </core/read-preference/>` in the {+mdb-server+} manual. |
| 101 | + |
| 102 | +Response |
| 103 | +-------- |
| 104 | + |
| 105 | +The ``command`` method returns a ``Mongo::Operation::Result`` that contains |
| 106 | +the response from the database for the given command. |
| 107 | + |
| 108 | +You can access the fields of the raw command response document by using the following |
| 109 | +methods: |
| 110 | + |
| 111 | +.. list-table:: |
| 112 | + :header-rows: 1 |
| 113 | + :widths: 30 70 |
| 114 | + |
| 115 | + * - Method |
| 116 | + - Description |
| 117 | + |
| 118 | + * - ``acknowledged?`` |
| 119 | + - Returns ``true`` if the server acknowledged the command, and ``false`` otherwise. |
| 120 | + |
| 121 | + * - ``inspect`` |
| 122 | + - Returns a formatted string representation of the command response. |
| 123 | + |
| 124 | + * - ``ok?`` |
| 125 | + - Returns ``true`` if the command succeeded, and ``false`` otherwise. If the ``ok?`` |
| 126 | + method returns ``false``, the driver raises a ``Mongo::Error::OperationFailure`` . |
| 127 | + |
| 128 | + * - ``cluster_time`` |
| 129 | + - Returns the cluster time reported in the server response. Cluster time is a |
| 130 | + logical time used for the ordering of operations. This field only |
| 131 | + applies to commands run on replica sets or sharded cluster. |
| 132 | + |
| 133 | + * - ``operation_time`` |
| 134 | + - Returns the logical time of the operation execution. |
| 135 | + |
| 136 | +For a full list of methods available on the ``Result`` object, see |
| 137 | +the `API documentation <{+api-root+}/Mongo/Operation/Result.html>`__. |
| 138 | + |
| 139 | +.. tip:: |
| 140 | + |
| 141 | + To learn more about logical time, see the Wikipedia entry on |
| 142 | + the :wikipedia:`logical clock <w/index.php?title=Logical_clock&oldid=1072010149>`. |
| 143 | + |
| 144 | +Example |
| 145 | +~~~~~~~ |
| 146 | + |
| 147 | +The following example runs the ``dbStats`` command to retrieve |
| 148 | +storage statistics for the ``sample_restaurants`` database, then prints the |
| 149 | +command results by using the ``inspect`` method: |
| 150 | + |
| 151 | +.. literalinclude:: /includes/databases-collections/run-command.rb |
| 152 | + :language: ruby |
| 153 | + :dedent: |
| 154 | + :start-after: start-print-command |
| 155 | + :end-before: end-print-command |
| 156 | + |
| 157 | +The output of this command includes information about the data stored in |
| 158 | +the database, as shown in the following code: |
| 159 | + |
| 160 | +.. code-block:: none |
| 161 | + :copyable: false |
| 162 | + |
| 163 | + {"db"=>"sample_restaurants", "collections"=>4, "views"=>0, "objects"=>18767, "avgObjSize"=>596.1911866574306, |
| 164 | + "dataSize"=>11188720, "storageSize"=>7528448, "totalFreeStorageSize"=>0, "numExtents"=>0, "indexes"=>6, |
| 165 | + "indexSize"=>1519616, "indexFreeStorageSize"=>0, "fileSize"=>0, "nsSizeMB"=>0, "ok"=>1} |
| 166 | + |
| 167 | +Additional Information |
| 168 | +---------------------- |
| 169 | + |
| 170 | +For more information about the concepts in this guide, see the following |
| 171 | +documentation in the {+mdb-server+} manual: |
| 172 | + |
| 173 | +- :manual:`db.runCommand() </reference/method/db.runCommand/>` |
| 174 | +- :manual:`Database Commands </reference/command/>` |
| 175 | +- :manual:`hello Command </reference/command/hello/>` |
| 176 | +- :manual:`dbStats Command </reference/command/dbStats/>` |
| 177 | + |
| 178 | +API Documentation |
| 179 | +~~~~~~~~~~~~~~~~~ |
| 180 | + |
| 181 | +To learn more about any of the methods or types discussed in this |
| 182 | +guide, see the following API documentation: |
| 183 | + |
| 184 | +- `command <{+api-root+}/Mongo/Database.html#command-instance_method>`_ |
0 commit comments