Docs Home → Develop Applications → MongoDB Manual
db.adminCommand()![](/docs/v5.3/assets/link.svg)
On this page
Important
mongosh Method
This is a mongosh
method. This is not the
documentation for Node.js
or other programming language specific
driver methods.
In most cases, mongosh
methods work the same way as
the legacy mongo
shell methods. However, some legacy
methods are unavailable in mongosh
.
For the legacy mongo
shell documentation, refer to the
documentation for the corresponding MongoDB Server release:
For MongoDB API drivers, refer to the language specific MongoDB driver documentation.
Definition![](/docs/v5.3/assets/link.svg)
db.adminCommand(command)
Provides a helper to run specified database commands against the
admin
database.ParameterTypeDescriptioncommand
document or stringA database command, specified either in document form or as a string. If specified as a string, the command cannot include any arguments.
Behavior![](/docs/v5.3/assets/link.svg)
db.adminCommand()
runs commands against the admin
database regardless of the database context in which it runs.
The following commands are equivalent:
db.getSiblingDB("admin").runCommand(<command>) db.adminCommand(<command>)
For a list of available administrative database commands, see Administration Commands.
Note
For a mongod
or mongos
running with
authorization
, the authorized user must have
the appropriate privileges to run the database command. See the
reference documentation for the command for more information on
security requirements.
Response![](/docs/v5.3/assets/link.svg)
The method returns a response document that contains the following fields:
Field | Description |
---|---|
<command result> | Result fields specific to the command that was run. |
ok | A number that indicates whether the command has succeeded ( 1 )
or failed (0 ). |
operationTime | The logical time of the operation. MongoDB uses the logical time to order operations. Only for replica sets and sharded clusters. If the command does not generate an oplog entry, e.g. a read
operation, then the operation does not advance the logical
clock. In this case,
For operations associated with causally consistent
sessions, MongoDB drivers use this time
to automatically set the Read Operations and |
$clusterTime | A document that returns the signed cluster time. Cluster time is a logical time used for ordering of operations. Only for replica sets and sharded clusters. For internal use only. The document contains the following fields:
|
Examples![](/docs/v5.3/assets/link.svg)
killOp![](/docs/v5.3/assets/link.svg)
The following example uses the db.adminCommand()
method to execute a killOp
command to terminate an
operation with opid 724
. killOp
is an administrative
command and must be run against the admin
database.
db.adminCommand( { "killOp": 1, "op": 724 } )
renameCollection![](/docs/v5.3/assets/link.svg)
The following example uses db.adminCommand()
to execute
the renameCollection
administrative database command
to rename the orders
collection in the test
database to
orders-2016
.
db.adminCommand( { renameCollection: "test.orders", to: "test.orders-2016" } )
createUser![](/docs/v5.3/assets/link.svg)
The following example uses the db.adminCommand()
method to
create a user named bruce
with the dbOwner
role on the
admin
database.
Tip
Starting in version 4.2 of the mongo
shell, you can
use the passwordPrompt()
method in conjunction with
various user authentication/management methods/commands to prompt
for the password instead of specifying the password directly in the
method/command call. However, you can still specify the password
directly as you would with earlier versions of the
mongo
shell.
db.adminCommand( { createUser: "bruce", pwd: passwordPrompt(), // or <cleartext password> roles: [ { role: "dbOwner", db: "admin" } ] } )