MongoDB Guide (translation) (6)-developer zone-database commands (1)

Source: Internet
Author: User
Tags findone mongodb server mongo shell

Each mongodb server supports multiple databases. For security and convenience management, each database is independent, and the data of each database is also stored separately.

A database consists of one or more sets, and documents (objects) are stored in the collection. An optional security certificate can control access.

 

Command

Introduction

The mongo database has a concept of database commands. Database commands tell the database to perform special operations or to request the status information of the current operation.

A command is sent to the database as a query for a special set named $ cmd. The database will put the command results in a separate document and return-use findOne () to check whether your driver has this function.

Generally, the command syntax is:

db.$cmd.findOne( { <commandname>: <value> [, options] } );

Shell provides auxiliary functions for it:

db.runCommand( { <commandname>: <value> [, options] } );

For example, to view the profile-level settings of our database, we can call:

> db.runCommand({profile:-1});
{
"was" : 0.0 ,
"ok" : 1.0
}

For many database commands, some driver implementations encapsulate some methods to make it easier to use. For example, mongo shell provides:

> db.getProfilingLevel()
0.0

Let's take a look at what this method has done:

> print( db.getProfilingLevel )
function () {
var res = this._dbCommand({profile:-1});
return res ? res.was : null;
}
> print( db._dbCommand )
function (cmdObj) {
return this.$cmd.findOne(cmdObj);
}

Many Commands have helper functions-view your driver documentation for more information.

 

Privileged command
Some operations are only prepared for the database administrator. These privileged operations may only be performed on a special database named admin.

> use admin;
> db.runCommand("shutdown"); // shut down the database

If the database variable is not set to admin, you can use adminCommand (in versions earlier than 1.8, It is _ adminCommand) to automatically switch to the correct database (only useful for the current operation ):

> db.adminCommand("shutdown");

(For this special command, there is also a shell helper function, db. shutdownServer .)

 

Obtain the help information of a command

You can use commandHelp in shell to obtain the help information of a command:

> db.commandHelp("datasize")
help for: datasize example: { datasize:"blog.posts", keyPattern:{x:1}, min:{x:10}, max:{x:55} }
NOTE: This command may take awhile to run

(Some commands do not currently have help information)

 

GetLog command

Log-related commands

Using the getLog command, you can get a list of log category variables and all messages for these categories. This command is introduced in 1.9.2.

> db.adminCommand({getLog:"*|global|<cat>"})

Retrieve category

Pass the "*" option to getLog to obtain all possible log categories:

> db.adminCommand({getLog:"*"})
{
"names" : [
"global",
"rs",
"startupWarnings" ],
"ok" : 1
}

The output indicates that there are three types of logs available: "global" (see below), copy group notifications, and start warnings.
"Global" category

This special "global" category contains all other categories. You can specify any category to restrict the returned rows.

> db.adminCommand({getLog:"global"})
{
"log" : [
"Thu Aug 18 13:57:05 [initandlisten] MongoDB starting : pid=654312892 port=27017
dbpath=/tmp/db1/ 64-bit host=localnose",
"Thu Aug 18 13:57:05 [initandlisten] recover : no journal files present, no recovery needed",
"Thu Aug 18 13:57:07 [websvr] admin web console waiting for connections on port 28017",
"Thu Aug 18 13:57:07 [initandlisten] waiting for connections on port 27017",
"Thu Aug 18 13:57:07 [initandlisten] connection accepted from 127.0.0.1:56703 #1",
"Thu Aug 18 13:57:07 [rsStart] replSet STARTUP2",
"Thu Aug 18 13:57:07 [rsSync] replSet SECONDARY",
"Thu Aug 18 13:57:10 [initandlisten] connection accepted from 127.0.0.1:56704 #2"
],
"ok" : 1
}

 

SetParameter command

Set runtime Parameters

This command allows you to change some internal options and some command line parameters at runtime.

This command is added in 1.8, but some options have changed since then. Pay attention to the available versions.

> db.adminCommand({setParameter:1, option1:true, ...})
// real example to increase logging
> db.adminCommand({setParameter:1, logLevel:4})
//disallow table/collection scans
> db.adminCommand({setParameter:1, notablescan:true})
{ "was" : false, "ok" : 1 }
> db.foo.insert({_id:1, name:false})
> db.foo.find()
{ "_id" : 1 , "name" : false }
> db.foo.find({name:true})
error: { "$err" : "table scans not allowed:test.foo", "code" : 10111 }
> db.adminCommand({setParameter:1, notablescan:false})
{ "was" : true, "ok" : 1 }
> db.foo.find({name:true})

Option

Option Value Description Available version
Syncdelay Number Memory Refresh Interval 1.8
Loglevel INTEGER (0-5) Set record level 1.8
Quiet True/false Record quiet mode 1.8
Notablescan True/false An error is triggered when a table scan is required. 1.8
Journalcommitinterval Number (1-500 ms) Log (Group) Submission window 1.9.1

Get parameter status
Just as you can set runtime parameters, you can also get them.

> db.adminCommand({getParameter:1, syncdelay:true})
{ "syncdelay" : 60, "ok" : 1 }

 

CloneCollection command

Copy a single set from one server to another server.

db.runCommand( { cloneCollection: <namespace>,
from:

Copy a set from one server to another. Do not use it on a single server, because it stores the target and source in the same db. collection (namespace ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.