db.adminCommand()

Definition

  • db.adminCommand(command)
  • Provides a helper to run specified database commands against the admin database.

ParameterTypeDescriptioncommanddocument or stringA database command, specified either in document formor as a string. If specified as a string, the command cannot includeany arguments.

Behavior

db.adminCommand runs commands against the admindatabase regardless of the database context in which it runs.The following commands are equivalent:

  1. db.getSiblingDB("admin").runCommand(<command>)
  2.  
  3. db.adminCommand(<command>)

For a list of available administrative database commands, seeAdministration Commands.

Note

For a mongod or mongos running withauthorization, the authorized user must havethe appropriate privileges to run the database command. See thereference documentation for the command for more information onsecurity requirements.

Response

The method returns a response document that contains the followingfields:

FieldDescription
Result fields specific to the command
okA number that indicates whether the command has succeeded(1) or failed (0).
operationTimeThe logical time of the performed operation, represented inMongoDB by the timestamp from the oplog entry. Only for replicasets and sharded clustersIf the command does not generate an oplog entry, e.g. a readoperation, then the operation does not advance the logicalclock. In this case, operationTime returns:- For read concern "local", the timestamp of themost recent entry in the oplog.- For read concern "majority" and"linearizable", the timestamp of the mostrecent majority-acknowledgedentry in the oplog.For operations associated with causally consistentsessions, MongoDB drivers use this timeto automatically set the Read Operations and afterClusterTime.New in version 3.6.
$clusterTimeA document that returns the signed cluster time. Cluster time is alogical time used for ordering of operations. Only for replicasets and sharded clusters. For internal use only.The document contains the following fields:- clusterTime: timestamp of the highest known cluster time for the member.- signature: a document that contains the hash of the cluster time and the idof the key used to sign the cluster time.New in version 3.6.

Examples

killOp

The following example uses the db.adminCommand()method to execute a killOp command to terminate anoperation with opid 724. killOp is an administrativecommand and must be run against the admin database.

  1. db.adminCommand( { "killOp": 1, "op": 724 } )

renameCollection

The following example uses db.adminCommand() to executethe renameCollection administrative database commandto rename the orders collection in the test database toorders-2016.

  1. db.adminCommand(
  2. {
  3. renameCollection: "test.orders",
  4. to: "test.orders-2016"
  5. }
  6. )

createUser

The following example uses the db.adminCommand() method tocreate a user named bruce with the dbOwner role on theadmin database.

Tip

Starting in version 4.2 of the mongo shell, you canuse the passwordPrompt() method in conjunction withvarious user authentication/management methods/commands to promptfor the password instead of specifying the password directly in themethod/command call. However, you can still specify the passworddirectly as you would with earlier versions of themongo shell.

  1. db.adminCommand(
  2. {
  3. createUser: "bruce",
  4. pwd: passwordPrompt(), // or <cleartext password>
  5. roles: [
  6. { role: "dbOwner", db: "admin" }
  7. ]
  8. }
  9. )