Skip to content

Commit 76c5922

Browse files
authored
Merge pull request #130 from mcmorisi/DOCSP-45176-run-command
DOCSP-45176: Run a command
2 parents 12625a2 + 9e431b4 commit 76c5922

File tree

6 files changed

+218
-4
lines changed

6 files changed

+218
-4
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ toc_landing_pages = [
55
"/connect",
66
"/write",
77
"/indexes",
8+
"/databases-collection",
89
"/security/authentication"
910
]
1011

source/databases-collection.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ Databases and Collections
1717
.. meta::
1818
:keywords: table, row, organize, storage
1919

20+
.. toctree::
21+
:titlesonly:
22+
:maxdepth: 1
23+
24+
Run a Command </databases-collections/run-command>
25+
2026
Overview
2127
--------
2228

@@ -122,8 +128,6 @@ To query for only the names of the collections in the database, call the
122128
(i.e. each collection object can be further queried for metadata), while
123129
``database.collection_names`` simply lists the collection names.
124130

125-
126-
127131
Delete a Collection
128132
-------------------
129133

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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>`_
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'bundler/inline'
2+
gemfile do
3+
source 'https://rubygems.org'
4+
gem 'mongo'
5+
end
6+
7+
uri = "<connection string>"
8+
9+
Mongo::Client.new(uri) do |client|
10+
# start-db
11+
database = client.use('sample_restaurants')
12+
# end-db
13+
14+
# start-hello
15+
client.database.command(hello: 1)
16+
# end-hello
17+
18+
# start-readpref
19+
client.database.command({hello: 1}, read: {mode: :secondary})
20+
# end-readpref
21+
22+
# start-print-command
23+
puts client.database.command({dbStats: 1}).first
24+
# end-print-command
25+
end

source/write/delete.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Sample Data
3232

3333
The examples in this guide use the ``restaurants`` collection in the ``sample_restaurants``
3434
database from the :atlas:`Atlas sample datasets </sample-data>`. To access this collection
35-
from your {+language+} application, create a ``MongoClient`` that connects to an Atlas cluster
35+
from your {+language+} application, create a ``Mongo::Client`` object that connects to an Atlas cluster
3636
and assign the following values to your ``database`` and ``collection`` variables:
3737

3838
.. literalinclude:: /includes/write/delete.rb

source/write/replace.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Sample Data
3535

3636
The examples in this guide use the ``restaurants`` collection in the ``sample_restaurants``
3737
database from the :atlas:`Atlas sample datasets </sample-data>`. To access this collection
38-
from your {+language+} application, create a ``MongoClient`` object that connects to an Atlas cluster
38+
from your {+language+} application, create a ``Mongo::Client`` object that connects to an Atlas cluster
3939
and assign the following values to your ``database`` and ``collection`` variables:
4040

4141
.. literalinclude:: /includes/write/replace.rb

0 commit comments

Comments
 (0)