Concept
A database command is a very special type of query. Document creation, updating, deletion, and querying are all part of the database command, and it also contains administrative tasks (such as shutting down the server and cloning the database), statistics, and performing aggregation operations.
RunCommand function
In general, it is possible to use only the functions provided by the shell, but it is helpful to know the commands underlying them. In particular, when using a version shell to connect to a new version of the database, the shell might not support some of the commands of the newer database, and then have to use the RunCommand function of the database command. How to use: Db.runcommand ({"Command name": value})
How Database commands work
The database command always returns a document that contains the "OK" key. If the value of "OK" is 1, the command execution succeeds. otherwise failed. If the "OK" value is 0, then the return document of the command will have an extra key-"errmsg". It is a string type used to describe the cause of the failure.
The above-mentioned "Database command" is a special type of query, because when the MongoDB server receives a command sent by a runcommand, it processes the request into a query operation of the $cmd collection. For example, Shel sends a db.runcommand ("Drop", "test") command (deleting a test collection) and the server is converted to DB. $cmd. FindOne ({"Drop": "Test"})
When some commands need to have administrator rights, and in the admin (MongoDB system comes from the database) database can be executed, you can not use the RunCommand function, instead of using the Admincommand function. Otherwise you will get an "Access Denied" error.
MongoDB Learning notes-database commands