w3resource

MongoDB: db.runCommand() method

db.runCommand

The db.runCommand() method is used to provide a helper to run specified database commands.

Syntax:

db.runCommand(command)

Parameters:

Name Description Required /
Optional
Type
command "A database command, specified either in document form or as a string. If specified as a string, db.runCommand() transforms the string into a document." Required document or string

Example: MongoDB: db.runCommand() method

The following command will display the helper to run $gt command.

db.runCommand('$gt()');

Output:

> db.runCommand('$gt()');
{
        "ok" : 0,
        "errmsg" : "no such cmd: $gt()",
        "code" : 59,
        "bad cmd" : {
                "$gt()" : 1
        }
}

Retrieve the restaurants data from here

Behavior

db.runCommand() runs the command in the context of the current database. Some commands are only applicable in the context of the admin database, and you must change your db object to before running these commands.

Previous: db.repairDatabase() method
Next: db.serverBuildInfo() method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mongodb/shell-methods/database/db-runCommand.php