@@ -45,18 +45,80 @@ statistics, initializing a replica set, or running an aggregation pipeline.
45
45
Execute a Command
46
46
-----------------
47
47
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 .
51
51
52
52
The following code shows how you can use the ``command()``
53
53
method on a ``Database`` to run the ``hello``
54
54
command, which returns information about the server:
55
55
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
+ }
60
122
61
123
To find a link to a full list of database commands and corresponding
62
124
parameters, see the :ref:`Additional Information section
@@ -76,11 +138,32 @@ The following example uses the ``cursor_command()`` method on the ``sample_mflix
76
138
database. It runs the ``find`` command on the ``movies`` database to filter by
77
139
documents in which the ``runtime``field has a value of ``11``.
78
140
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
+ ...
84
167
85
168
Before you run a command, learn about the response format of the command so that
86
169
you can handle the ``CommandCursor`` accordingly.
0 commit comments