发布于 2015-09-14 14:50:01 | 104 次阅读 | 评论: 0 | 来源: 网络整理

The MongoDB command interface provides access to all non CRUD database operations. Fetching server stats, initializing a replica set, and running a map-reduce job are all accomplished with commands.

See 数据库命令快参 for list of all commands sorted by function, and 数据库命令 for a list of all commands sorted alphabetically.

Database Command Form

You specify a command first by constructing a standard BSON document whose first key is the name of the command. For example, specify the isMaster command using the following BSON document:

{ isMaster: 1 }

Issue Commands

The mongo shell provides a helper method for running commands called db.runCommand(). The following operation in mongo runs the above command:

db.runCommand( { isMaster: 1 } )

Many drivers provide an equivalent for the db.runCommand() method. Internally, running commands with db.runCommand() is equivalent to a special query against the $cmd collection.

Many common commands have their own shell helpers or wrappers in the mongo shell and drivers, such as the db.isMaster() method in the mongo JavaScript shell.

admin Database Commands

You must run some commands on the admin database. Normally, these operations resemble the followings:

use admin
db.runCommand( {buildInfo: 1} )

However, there’s also a command helper that automatically runs the command in the context of the admin database:

db._adminCommand( {buildInfo: 1} )

Command Responses

All commands return, at minimum, a document with an ok field indicating whether the command has succeeded:

{ 'ok': 1 }

Failed commands return the ok field with a value of 0.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务