-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
use-database-commands.txt
88 lines (60 loc) · 2.28 KB
/
use-database-commands.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
:orphan:
=====================
Use Database Commands
=====================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
The MongoDB command interface provides access to all :term:`non CRUD
<crud>` database operations. Fetching server stats, initializing a
replica set, and running a map-reduce job are all accomplished with
commands.
See :doc:`/reference/command` for list of all commands sorted by
function.
Database Command Form
---------------------
You specify a command first by constructing a standard :term:`BSON`
document whose first key is the name of the command. For example,
specify the :dbcommand:`hello` command using the following
:term:`BSON` document:
.. code-block:: javascript
{ hello: 1 }
.. _issue-commands:
Issue Commands
--------------
The :binary:`~bin.mongo` shell provides a helper method for running
commands called :method:`db.runCommand()`. The following operation in
:binary:`~bin.mongo` runs the above command:
.. code-block:: javascript
db.runCommand( { hello: 1 } )
Many :ecosystem:`drivers </drivers>` provide an equivalent for
the :method:`db.runCommand()` method. Internally, running commands
with :method:`db.runCommand()` is equivalent to a special query
against the :term:`$cmd` collection.
Many common commands have their own shell helpers or wrappers in the
:binary:`~bin.mongo` shell and drivers, such as the
:method:`db.hello()` method in the :binary:`~bin.mongo` JavaScript
shell.
You can use the ``maxTimeMS`` option to specify a time limit for the
execution of a command, see :ref:`terminate-long-running-commands` for
more information on operation termination.
.. _admin-command:
``admin`` Database Commands
---------------------------
You must run some commands on the :term:`admin database`. Normally,
these operations resemble the followings:
.. code-block:: javascript
use admin
db.runCommand( {buildInfo: 1} )
However, there's also a command helper that automatically runs the
command in the context of the ``admin`` database:
.. code-block:: javascript
db.adminCommand( {buildInfo: 1} )
Command Responses
-----------------
For all commands, MongoDB returns a response document that contains the
following fields:
.. include:: /includes/fact-runCommand-results.rst