Skip to content

Commit 54d862d

Browse files
authored
(DOCSP-45011) Run a Command (#70) (#73)
* (DOCSP-45011) Add page for Run COmmand. * (DOCSP-45011) Test. * (DOCSP-45011) Add page to toctree * (DOCSP-45011) Edits. * (DOCSP-45011) Add code example. * (DOCSP-45011) Edits. * (DOCSP-45011) Execute a command section edits. * (DOCSP-45011) Edit output. * (DOCSP-45011) Formatting. * (DOCSP-45011) Formatting output. * (DOCSP-45011) Formatting output. * (DOCSP-45011) Read preference. * (DOCSP-45011) Read Preferences example. * (DOCSP-45011) wip * (DOCSP-45011) Formatitng output. * (DOCSP-45011) Remove read preferences. * (DOCSP-45011) Remove sample data section. * (DOCSP-45011) Edits. * (DOCSP-45011) Update link. * (DOCSP-45011) Edit intro. * (DOCSP-45011) Fix link. * (DOCSP-45011) Fix link. * (DOCSP-45011) @jordan-smith721 Review edits.
1 parent a544cae commit 54d862d

File tree

3 files changed

+234
-0
lines changed

3 files changed

+234
-0
lines changed

source/includes/run-command.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
3+
#include <bsoncxx/builder/basic/document.hpp>
4+
#include <bsoncxx/json.hpp>
5+
#include <mongocxx/client.hpp>
6+
#include <mongocxx/instance.hpp>
7+
#include <mongocxx/uri.hpp>
8+
9+
using bsoncxx::builder::basic::kvp;
10+
using bsoncxx::builder::basic::make_document;
11+
12+
int main() {
13+
mongocxx::instance instance;
14+
mongocxx::uri uri("<connection string>");
15+
mongocxx::client client(uri);
16+
17+
18+
{
19+
// start-run-hello
20+
auto db = client["my_database"];
21+
22+
auto command = make_document(kvp("hello" , 1));
23+
auto result = db.run_command(command.view());
24+
25+
std::cout << bsoncxx::to_json(result) << std::endl;
26+
// end-run-hello
27+
}
28+
{
29+
// start-run-connectionStatus
30+
auto db = client["my_database"];
31+
32+
auto command = make_document(kvp("connectionStatus" , 1), kvp("showPrivileges", true));
33+
auto result = db.run_command(command.view());
34+
35+
std::cout << bsoncxx::to_json(result) << std::endl;
36+
// end-run-connectionStatus
37+
}
38+
}

source/index.txt

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ MongoDB C++ Driver
1616
Write Data </write>
1717
Indexes </indexes>
1818
Aggregation </aggregation>
19+
Run a Command </run-command>
1920
Security </security>
2021
Specialized Data Formats </data-formats>
2122
C++17 Polyfill </polyfill-selection>
@@ -30,6 +31,7 @@ MongoDB C++ Driver
3031
API Documentation <https://mongocxx.org/api/mongocxx-{+full-version+}>
3132
Driver Source <https://github.com/mongodb/mongo-cxx-driver>
3233

34+
3335
Overview
3436
--------
3537

source/run-command.txt

+194
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
.. cpp-run-command:
2+
3+
=============
4+
Run a Command
5+
=============
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
Overview
14+
--------
15+
16+
In this guide, you can learn how to run a database command with the
17+
{+driver-short+}. You can use database commands to perform a variety of
18+
administrative and diagnostic tasks, such as fetching server statistics,
19+
initializing a replica set, or running an aggregation pipeline.
20+
21+
.. important:: Prefer Driver Methods to Database Commands
22+
23+
The driver provides wrapper methods for many database commands.
24+
We recommend using driver methods instead of executing database
25+
commands when possible.
26+
27+
To perform administrative tasks, use the :mongosh:`MongoDB Shell </>`
28+
instead of the {+driver-short+}. Calling the ``db.runCommand()``
29+
method inside the shell is the preferred method to issue database
30+
commands, as it provides a consistent interface between the shell and
31+
drivers.
32+
33+
Execute a Command
34+
-----------------
35+
36+
To run a database command, call the ``run_command()`` execution method on a ``mongocxx::database`` instance
37+
and pass in a document that specifies the command and any relevant arguments.
38+
The method returns the result of the command as a ``bsoncxx::document::value`` object.
39+
40+
You can use the ``run_command()`` method with any database command.
41+
For a full list of database commands and corresponding parameters, see :manual:`Database Commands </reference/command/>` in the {+mdb-server+} manual.
42+
43+
The following example shows how you can use the ``run_command()``
44+
method to run the ``hello`` command on a database, which returns information about
45+
the current member's role in the replica set:
46+
47+
.. io-code-block::
48+
:copyable: true
49+
50+
.. input:: /includes/run-command.cpp
51+
:start-after: start-run-hello
52+
:end-before: end-run-hello
53+
:language: cpp
54+
:dedent:
55+
56+
.. output::
57+
:language: cli
58+
:visible: false
59+
60+
{
61+
"topologyVersion" : {
62+
"processId" : ...,
63+
"counter" : ...
64+
},
65+
"hosts" : [ ... ],
66+
"setName" : ...,
67+
"setVersion" : ...,
68+
"isWritablePrimary" : ...,
69+
"secondary" : ...,
70+
"primary" : ...,
71+
"tags" : {
72+
"region" : ...,
73+
"availabilityZone" : ...,
74+
"provider" : ...,
75+
"workloadType" : ...,
76+
"nodeType" : ...,
77+
"diskState" : ...
78+
},
79+
"me" : ...,
80+
"electionId" : ...,
81+
"lastWrite" : ...,
82+
"lastWriteDate" : ...,
83+
"majorityOpTime" : ...,
84+
"majorityWriteDate" : ...,
85+
"maxBsonObjectSize" : ...,
86+
"maxMessageSizeBytes" : ...,
87+
"maxWriteBatchSize" : ...,
88+
"localTime" : ...,
89+
"logicalSessionTimeoutMinutes" : ...,
90+
"connectionId" : ...,
91+
"minWireVersion" : ...,
92+
"maxWireVersion" : ...,
93+
"readOnly" : ...,
94+
"ok" : ...,
95+
"$clusterTime" : ...,
96+
"signature" : ...
97+
}
98+
99+
.. cpp-command-options:
100+
101+
Command Options
102+
---------------
103+
104+
To customize command execution behavior, you can set options in the command
105+
document that you pass to the ``run_command()`` method. To learn more about
106+
a command and the options that it accepts, locate the command and follow the
107+
corresponding link on the :manual:`Database Commands </reference/command/>` page
108+
in the {+mdb-server+} manual.
109+
110+
For example, you can instruct the ``connectionStatus`` command
111+
to return the full set of privileges that currently-authenticated users possess by setting the ``showPrivileges``
112+
option to ``true`` in the command document:
113+
114+
.. io-code-block::
115+
:copyable: true
116+
117+
.. input:: /includes/run-command.cpp
118+
:start-after: start-run-connectionStatus
119+
:end-before: end-run-connectionStatus
120+
:language: cpp
121+
:dedent:
122+
123+
.. output::
124+
:language: cli
125+
:visible: false
126+
127+
{
128+
"authInfo" : { "authenticatedUsers" : [ { "user" : ..., "db" : ... } ],
129+
"authenticatedUserRoles" : [ { "role" : ..., "db" : ... } ],
130+
"authenticatedUserPrivileges" : [
131+
{ "resource" : { "db" : "", "collection" : "" }, "actions" : [ ... ] },
132+
{ "resource" : { "db" : "config", "collection" : "system.sessions" }, "actions" : [ ... ] },
133+
...,
134+
{ "resource" : { "db" : "", "collection" : "" }, "actions" : [ ... ] }
135+
]
136+
},
137+
"ok" : 1
138+
}
139+
140+
Response
141+
--------
142+
143+
The ``run_command()`` method returns a ``bsoncxx::document::value`` object that contains
144+
the response from the database after the command has been executed. Each
145+
database command performs a different function, so the response content
146+
can vary across commands. However, every response contains documents
147+
with the following fields:
148+
149+
.. list-table::
150+
:header-rows: 1
151+
:widths: 30 70
152+
153+
* - Field
154+
- Description
155+
156+
* - <command result>
157+
- Provides fields specific to the database command. For example,
158+
``count`` returns the ``n`` field and ``explain`` returns the
159+
``queryPlanner`` field.
160+
161+
* - ``ok``
162+
- Indicates whether the command has succeeded (``1``)
163+
or failed (``0``).
164+
165+
* - ``operationTime``
166+
- Indicates the logical time of the operation. MongoDB uses the
167+
logical time to order operations.
168+
169+
* - ``$clusterTime``
170+
- Provides a document that returns the signed cluster time. Cluster time is a
171+
logical time used for ordering of operations.
172+
173+
The document contains the following fields:
174+
175+
- ``clusterTime``, which is the timestamp of the highest known cluster time for the member.
176+
- ``signature``, which is a document that contains the hash of the cluster time and the ID
177+
of the key used to sign the cluster time.
178+
179+
.. _addl-info-runcommand:
180+
181+
Additional Information
182+
----------------------
183+
184+
For more information about the concepts in this guide, see the following documentation:
185+
186+
- :manual:`db.runCommand() </reference/method/db.runCommand/>`
187+
- :manual:`Database Commands </reference/command/>`
188+
- :manual:`hello Command </reference/command/hello/>`
189+
- :manual:`connectionStatus Command </reference/command/connectionStatus/>`
190+
191+
To learn more about the methods or types discussed in this
192+
guide, see the following API documentation:
193+
194+
- `run_command() <{+api+}/classmongocxx_1_1v__noabi_1_1database.html#a1e11c0874c945f8bb9ca39f1a30c9271>`__

0 commit comments

Comments
 (0)