@@ -45,18 +45,80 @@ statistics, initializing a replica set, or running an aggregation pipeline.
4545Execute a Command
4646-----------------
4747
48- To run a database command, you must specify the command and any relevant
49- parameters in a command document, then pass the command document to the
50- ``command()`` method.
48+ You can use the `` command()`` method to run a database command. You must specify
49+ the command and any relevant parameters. These can be passed as strings or as
50+ a ``dict``. The method will return the result of the command that was run .
5151
5252The following code shows how you can use the ``command()``
5353method on a ``Database`` to run the ``hello``
5454command, which returns information about the server:
5555
56- .. literalinclude:: /includes/run-command.py
57- :language: python
58- :start-after: start-hello
59- :end-before: end-hello
56+ .. io-code-block::
57+ :copyable: true
58+
59+ .. input:: /includes/run-command.py
60+ :language: python
61+ :start-after: start-hello
62+ :end-before: end-hello
63+
64+ .. output::
65+ :language: json
66+ :visible: false
67+
68+ {
69+ 'topologyVersion': {
70+ 'processId': ObjectId('6724d211d6b98fa1931e8616'),
71+ 'counter': 6
72+ },
73+ 'hosts': ['cluster0-shard-00-00.fxoii.mongodb.net:27017',
74+ 'cluster0-shard-00-01.fxoii.mongodb.net:27017',
75+ 'cluster0-shard-00-02.fxoii.mongodb.net:27017'],
76+ 'setName': 'atlas-13l6uw-shard-0',
77+ 'setVersion': 114,
78+ 'isWritablePrimary': True,
79+ 'secondary': False,
80+ 'primary': 'cluster0-shard-00-02.fxoii.mongodb.net:27017',
81+ 'tags': {
82+ 'workloadType': 'OPERATIONAL',
83+ 'diskState': 'READY',
84+ 'region': 'US_EAST_1',
85+ 'provider': 'AWS',
86+ 'nodeType': 'ELECTABLE',
87+ 'availabilityZone': 'use1-az5'
88+ },
89+ 'me': 'cluster0-shard-00-02.fxoii.mongodb.net:27017',
90+ 'electionId': ObjectId('7fffffff00000000000000e3'),
91+ 'lastWrite': {
92+ 'opTime': {
93+ 'ts': Timestamp(1730486145, 22),
94+ 't': 227
95+ },
96+ 'lastWriteDate': datetime.datetime(2024, 11, 1, 18, 35, 45),
97+ 'majorityOpTime': {
98+ 'ts': Timestamp(1730486145, 22),
99+ 't': 227
100+ },
101+ 'majorityWriteDate': datetime.datetime(2024, 11, 1, 18, 35, 45)
102+ },
103+ 'maxBsonObjectSize': 16777216,
104+ 'maxMessageSizeBytes': 48000000,
105+ 'maxWriteBatchSize': 100000,
106+ 'localTime': datetime.datetime(2024, 11, 1, 18, 35, 45, 309000),
107+ 'logicalSessionTimeoutMinutes': 30,
108+ 'connectionId': 23889,
109+ 'minWireVersion': 0,
110+ 'maxWireVersion': 21,
111+ 'readOnly': False,
112+ 'ok': 1.0,
113+ '$clusterTime': {
114+ 'clusterTime': Timestamp(1730486145, 22),
115+ 'signature': {
116+ 'hash': b"\x1a\xf7{>q%F\xc2\x89\x15\x13W29\x91\xaae'~\xe4",
117+ 'keyId': 7379292132843978793
118+ }
119+ },
120+ 'operationTime': Timestamp(1730486145, 22)
121+ }
60122
61123To find a link to a full list of database commands and corresponding
62124parameters, see the :ref:`Additional Information section
@@ -76,11 +138,32 @@ The following example uses the ``cursor_command()`` method on the ``sample_mflix
76138database. It runs the ``find`` command on the ``movies`` database to filter by
77139documents in which the ``runtime``field has a value of ``11``.
78140
79- .. literalinclude:: /includes/run-command.py
80- :language: python
81- :dedent:
82- :start-after: start-cursor-command
83- :end-before: end-cursor-command
141+ .. io-code-block::
142+ :copyable: true
143+
144+ .. input:: /includes/run-command.py
145+ :language: python
146+ :dedent:
147+ :start-after: start-cursor-command
148+ :end-before: end-cursor-command
149+
150+ .. output::
151+ :language: json
152+ :visible: false
153+
154+ {
155+ '_id': ObjectId('573a1390f29313caabcd42e8'),
156+ 'runtime': 11,
157+ 'title': 'The Great Train Robbery',
158+ ...
159+ },
160+ {
161+ {'_id': ObjectId('573a1394f29313caabce0f10'),
162+ 'runtime': 11,
163+ 'title': 'Glas',
164+ ...
165+ },
166+ ...
84167
85168Before you run a command, learn about the response format of the command so that
86169you can handle the ``CommandCursor`` accordingly.
0 commit comments