命令是mongoDB中一个比较特殊的操作。
在mongoDB中执行命令有两种常用的方法,
1. runCommand({$key : $value})
如:
> db.runCommand({"shutdown" : 1})
2. 查询$cmd这个特殊的collection
如:
> db.$cmd.findOne({"shutdown" : 1})
列出当前mongoDB中可以支持的命令,可以通过调用db.listCommands()或者是查看浏览器 run “mongod --rest” and then visit http://localhost:28017/_commands in your web browser.
如:
> db.listCommands()
_recvChunkCommit: no-lock adminOnly slaveOk
internal should not be calling this directly
_recvChunkStart: write-lock adminOnly slaveOk
internal should not be calling this directly
_recvChunkStatus: no-lock adminOnly slaveOk
internal should not be calling this directly
_testDistLockWithSyncCluster: no-lock adminOnly slaveOk
should not be calling this directly
_transferMods: no-lock adminOnly slaveOk
internal should not be calling this directly
applyOps: write-lock
examples: { applyOps : [ ] , preCondition : [ { ns : ... , q : ... , res : ... } ] }
assertInfo: write-lock
check if any asserts have occurred on the server
authenticate: write-lock
internal
availablequeryoptions: no-lock
no help defined
buildInfo: no-lock adminOnly slaveOk
get version #, etc.
{ buildinfo:1 }
captrunc: write-lock
no help defined
clean: write-lock
internal
clone: write-lock
clone this database from an instance of the db on another host
{ clone : "host13" }
cloneCollection: no-lock
{ cloneCollection: <namespace>, from: <host> [,query: <query_filter>] [,copyIndexes:<bool>] }
Copies a collection from one server to another. Do not use on a single server as the destination is placed at the same db.collection (namespace) as the source.
Warning: the local copy of 'ns' is emptied before the copying begins. Any existing data will be lost there.
cloneCollectionAsCapped: write-lock
{ cloneCollectionAsCapped:<fromName>, toCollection:<toName>, size:<sizeInBytes> }
closeAllDatabases: write-lock adminOnly slaveOk
Close all database files.
A new request will cause an immediate reopening; thus, this is mostly for testing purposes.
collStats: read-lock
{ collStats:"blog.posts" , scale : 1 } scale divides sizes e.g. for KB use 1024
connPoolStats: no-lock
stats about connection pool
connPoolSync: no-lock
internal
convertToCapped: write-lock
{ convertToCapped:<fromCollectionName>, size:<sizeInBytes> }
copydb: write-lock adminOnly slaveOk
copy a database from another host to this host
usage: {copydb: 1, fromhost: <hostname>, fromdb: <db>, todb: <db>[, username: <username>, nonce: <nonce>, key: <key>]}
copydbgetnonce: write-lock adminOnly slaveOk
get a nonce for subsequent copy db request from secure server
usage: {copydbgetnonce: 1, fromhost: <hostname>}
count: read-lock
count objects in collection
create: write-lock
create a collection
cursorInfo: no-lock
example: { cursorInfo : 1 }
dataSize: read-lock
determine data size for a set of data in a certain range
example: { datasize:"blog.posts", keyPattern:{x:1}, min:{x:10}, max:{x:55} }
keyPattern, min, and max parameters are optional.
note: This command may take a while to run
dbHash: read-lock
no help defined
dbStats: read-lock
example: { dbstats:1 }
diagLogging: write-lock adminOnly slaveOk
http://www.mongodb.org/display/DOCS/Monitoring+and+Diagnostics#MonitoringandDiagnostics-DatabaseRecord%2FReplay
distinct: read-lock
{ distinct : 'collection name' , key : 'a.b' , query : {} }
driverOIDTest: no-lock
no help defined
drop: write-lock
drop a collection
{drop : <collectionName>}
dropDatabase: write-lock
drop (delete) this database
dropIndexes: write-lock
drop indexes for a collection
emptycapped: write-lock
no help defined
eval: no-lock
Evaluate javascript at the server.
http://www.mongodb.org/display/DOCS/Server-side+Code+Execution
features: read-lock
return on build level feature settings
filemd5: read-lock
example: { filemd5 : ObjectId(aaaaaaa) , root : "fs" }
findAndModify: write-lock
{ findandmodify: "collection", query: {processed:false}, update: {$set: {processed:true}}, new: true}
{ findandmodify: "collection", query: {processed:false}, remove: true, sort: {priority:-1}}
Either update or remove is required, all other fields have default values.
Output is in the "value" field
forcedead: write-lock adminOnly slaveOk
internal
forceerror: no-lock
for testing purposes only. forces a user assertion exception
fsync: write-lock adminOnly slaveOk
http://www.mongodb.org/display/DOCS/fsync+Command
geoNear: read-lock
http://www.mongodb.org/display/DOCS/Geospatial+Indexing#GeospatialIndexing-geoNearCommand
geoSearch: read-lock
no help defined
geoWalk: read-lock
no help defined
getCmdLineOpts: no-lock adminOnly slaveOk
get argv
getLastError: no-lock
return error status of the last operation on this connection
getPrevError: no-lock
check for errors since last reseterror commandcal
getShardVersion: no-lock adminOnly slaveOk
example: { getShardVersion : 'alleyinsider.foo' }
getnonce: no-lock
internal
getoptime: no-lock
internal
godinsert: write-lock
internal. for testing only.
group: read-lock
http://www.mongodb.org/display/DOCS/Aggregation
handshake: no-lock
internal
isMaster: no-lock
Check if this server is primary for a replica pair/set; also if it is --master or --slave in simple master/slave setups.
{ isMaster : 1 }
isinitialsynccomplete: write-lock
no help defined
listCommands: no-lock
get a list of all db commands
listDatabases: read-lock adminOnly slaveOk
list databases on this server
logRotate: no-lock adminOnly slaveOk
no help defined
logout: no-lock
de-authenticate
mapReduce: no-lock
Run a map/reduce operation on the server.
Note this is used for aggregation, not querying, in MongoDB.
http://www.mongodb.org/display/DOCS/MapReduce
mapreduce.shardedfinish: no-lock
no help defined
medianKey: read-lock
Internal command.
example: { medianKey:"blog.posts", keyPattern:{x:1}, min:{x:10}, max:{x:55} }
NOTE: This command may take a while to run
moveChunk: no-lock adminOnly slaveOk
should not be calling this directly
negotiatemaster: write-lock adminOnly slaveOk
no help defined
ping: no-lock
a way to check that the server is alive. responds immediately even if server is in a db lock.
profile: write-lock
enable or disable performance profiling
{ profile : <n> }
0=off 1=log slow ops 2=log all
http://www.mongodb.org/display/DOCS/Database+Profiler
reIndex: write-lock
re-index a collection
renameCollection: write-lock adminOnly slaveOk
example: { renameCollection: foo.a, to: bar.b }
repairDatabase: write-lock
repair database. also compacts. note: slow.
replSetElect: no-lock adminOnly slaveOk
internal
replSetFreeze: no-lock adminOnly slaveOk
Enable / disable failover for the set - locks current primary as primary even if issues occur.
For use during system maintenance.
{ replSetFreeze : <bool> }
http://www.mongodb.org/display/DOCS/Replica+Set+Commands
replSetFresh: no-lock adminOnly slaveOk
internal
replSetGetRBID: no-lock adminOnly slaveOk
internal
replSetGetStatus: no-lock adminOnly slaveOk
Report status of a replica set from the POV of this server
{ replSetGetStatus : 1 }
http://www.mongodb.org/display/DOCS/Replica+Set+Commands
replSetHeartbeat: no-lock
internal
replSetInitiate: no-lock adminOnly slaveOk
Initiate/christen a replica set.
http://www.mongodb.org/display/DOCS/Replica+Set+Commands
replSetReconfig: no-lock adminOnly slaveOk
Adjust configuration of a replica set
{ replSetReconfig : config_object }
http://www.mongodb.org/display/DOCS/Replica+Set+Commands
replSetStepDown: no-lock adminOnly slaveOk
Step down as primary. Will not try to reelect self or 1 minute.
(If another member with same priority takes over in the meantime, it will stay primary.)
http://www.mongodb.org/display/DOCS/Replica+Set+Commands
replSetTest: no-lock adminOnly slaveOk
Just for regression tests.
replacePeer: write-lock adminOnly slaveOk
replace a node in a replica pair
resetError: no-lock
reset error state (used with getpreverror)
resync: write-lock adminOnly slaveOk
resync (from scratch) an out of date replica slave.
http://www.mongodb.org/display/DOCS/Master+Slave
serverStatus: no-lock
returns lots of administrative server statistics
setShardVersion: write-lock adminOnly slaveOk
example: { setShardVersion : 'alleyinsider.foo' , version : 1 , configdb : '' }
shardingState: write-lock adminOnly slaveOk
no help defined
shutdown: write-lock adminOnly slaveOk
shutdown the database. must be ran against admin db and either (1) ran from localhost or (2) authenticated.
sleep: no-lock adminOnly slaveOk
internal testing command. Makes db block (in a read lock) for 100 seconds
w:true write lock
splitVector: read-lock
Internal command.
example: { splitVector : "myLargeCollection" , keyPattern : {x:1} , maxChunkSize : 200 }
maxChunkSize unit in MBs
NOTE: This command may take a while to run
top: read-lock adminOnly slaveOk
usage by collection
unsetSharding: no-lock adminOnly slaveOk
example: { unsetSharding : 1 }
validate: read-lock
Validate contents of a namespace by scanning its data structures for correctness. Slow.
whatsmyuri: no-lock
{whatsmyuri:1}
writebacklisten: no-lock adminOnly slaveOk
internal
在mongoDB中执行命令有两种常用的方法,
1. runCommand({$key : $value})
如:
> db.runCommand({"shutdown" : 1})
2. 查询$cmd这个特殊的collection
如:
> db.$cmd.findOne({"shutdown" : 1})
列出当前mongoDB中可以支持的命令,可以通过调用db.listCommands()或者是查看浏览器 run “mongod --rest” and then visit http://localhost:28017/_commands in your web browser.
如:
> db.listCommands()
_recvChunkCommit: no-lock adminOnly slaveOk
internal should not be calling this directly
_recvChunkStart: write-lock adminOnly slaveOk
internal should not be calling this directly
_recvChunkStatus: no-lock adminOnly slaveOk
internal should not be calling this directly
_testDistLockWithSyncCluster: no-lock adminOnly slaveOk
should not be calling this directly
_transferMods: no-lock adminOnly slaveOk
internal should not be calling this directly
applyOps: write-lock
examples: { applyOps : [ ] , preCondition : [ { ns : ... , q : ... , res : ... } ] }
assertInfo: write-lock
check if any asserts have occurred on the server
authenticate: write-lock
internal
availablequeryoptions: no-lock
no help defined
buildInfo: no-lock adminOnly slaveOk
get version #, etc.
{ buildinfo:1 }
captrunc: write-lock
no help defined
clean: write-lock
internal
clone: write-lock
clone this database from an instance of the db on another host
{ clone : "host13" }
cloneCollection: no-lock
{ cloneCollection: <namespace>, from: <host> [,query: <query_filter>] [,copyIndexes:<bool>] }
Copies a collection from one server to another. Do not use on a single server as the destination is placed at the same db.collection (namespace) as the source.
Warning: the local copy of 'ns' is emptied before the copying begins. Any existing data will be lost there.
cloneCollectionAsCapped: write-lock
{ cloneCollectionAsCapped:<fromName>, toCollection:<toName>, size:<sizeInBytes> }
closeAllDatabases: write-lock adminOnly slaveOk
Close all database files.
A new request will cause an immediate reopening; thus, this is mostly for testing purposes.
collStats: read-lock
{ collStats:"blog.posts" , scale : 1 } scale divides sizes e.g. for KB use 1024
connPoolStats: no-lock
stats about connection pool
connPoolSync: no-lock
internal
convertToCapped: write-lock
{ convertToCapped:<fromCollectionName>, size:<sizeInBytes> }
copydb: write-lock adminOnly slaveOk
copy a database from another host to this host
usage: {copydb: 1, fromhost: <hostname>, fromdb: <db>, todb: <db>[, username: <username>, nonce: <nonce>, key: <key>]}
copydbgetnonce: write-lock adminOnly slaveOk
get a nonce for subsequent copy db request from secure server
usage: {copydbgetnonce: 1, fromhost: <hostname>}
count: read-lock
count objects in collection
create: write-lock
create a collection
cursorInfo: no-lock
example: { cursorInfo : 1 }
dataSize: read-lock
determine data size for a set of data in a certain range
example: { datasize:"blog.posts", keyPattern:{x:1}, min:{x:10}, max:{x:55} }
keyPattern, min, and max parameters are optional.
note: This command may take a while to run
dbHash: read-lock
no help defined
dbStats: read-lock
example: { dbstats:1 }
diagLogging: write-lock adminOnly slaveOk
http://www.mongodb.org/display/DOCS/Monitoring+and+Diagnostics#MonitoringandDiagnostics-DatabaseRecord%2FReplay
distinct: read-lock
{ distinct : 'collection name' , key : 'a.b' , query : {} }
driverOIDTest: no-lock
no help defined
drop: write-lock
drop a collection
{drop : <collectionName>}
dropDatabase: write-lock
drop (delete) this database
dropIndexes: write-lock
drop indexes for a collection
emptycapped: write-lock
no help defined
eval: no-lock
Evaluate javascript at the server.
http://www.mongodb.org/display/DOCS/Server-side+Code+Execution
features: read-lock
return on build level feature settings
filemd5: read-lock
example: { filemd5 : ObjectId(aaaaaaa) , root : "fs" }
findAndModify: write-lock
{ findandmodify: "collection", query: {processed:false}, update: {$set: {processed:true}}, new: true}
{ findandmodify: "collection", query: {processed:false}, remove: true, sort: {priority:-1}}
Either update or remove is required, all other fields have default values.
Output is in the "value" field
forcedead: write-lock adminOnly slaveOk
internal
forceerror: no-lock
for testing purposes only. forces a user assertion exception
fsync: write-lock adminOnly slaveOk
http://www.mongodb.org/display/DOCS/fsync+Command
geoNear: read-lock
http://www.mongodb.org/display/DOCS/Geospatial+Indexing#GeospatialIndexing-geoNearCommand
geoSearch: read-lock
no help defined
geoWalk: read-lock
no help defined
getCmdLineOpts: no-lock adminOnly slaveOk
get argv
getLastError: no-lock
return error status of the last operation on this connection
getPrevError: no-lock
check for errors since last reseterror commandcal
getShardVersion: no-lock adminOnly slaveOk
example: { getShardVersion : 'alleyinsider.foo' }
getnonce: no-lock
internal
getoptime: no-lock
internal
godinsert: write-lock
internal. for testing only.
group: read-lock
http://www.mongodb.org/display/DOCS/Aggregation
handshake: no-lock
internal
isMaster: no-lock
Check if this server is primary for a replica pair/set; also if it is --master or --slave in simple master/slave setups.
{ isMaster : 1 }
isinitialsynccomplete: write-lock
no help defined
listCommands: no-lock
get a list of all db commands
listDatabases: read-lock adminOnly slaveOk
list databases on this server
logRotate: no-lock adminOnly slaveOk
no help defined
logout: no-lock
de-authenticate
mapReduce: no-lock
Run a map/reduce operation on the server.
Note this is used for aggregation, not querying, in MongoDB.
http://www.mongodb.org/display/DOCS/MapReduce
mapreduce.shardedfinish: no-lock
no help defined
medianKey: read-lock
Internal command.
example: { medianKey:"blog.posts", keyPattern:{x:1}, min:{x:10}, max:{x:55} }
NOTE: This command may take a while to run
moveChunk: no-lock adminOnly slaveOk
should not be calling this directly
negotiatemaster: write-lock adminOnly slaveOk
no help defined
ping: no-lock
a way to check that the server is alive. responds immediately even if server is in a db lock.
profile: write-lock
enable or disable performance profiling
{ profile : <n> }
0=off 1=log slow ops 2=log all
http://www.mongodb.org/display/DOCS/Database+Profiler
reIndex: write-lock
re-index a collection
renameCollection: write-lock adminOnly slaveOk
example: { renameCollection: foo.a, to: bar.b }
repairDatabase: write-lock
repair database. also compacts. note: slow.
replSetElect: no-lock adminOnly slaveOk
internal
replSetFreeze: no-lock adminOnly slaveOk
Enable / disable failover for the set - locks current primary as primary even if issues occur.
For use during system maintenance.
{ replSetFreeze : <bool> }
http://www.mongodb.org/display/DOCS/Replica+Set+Commands
replSetFresh: no-lock adminOnly slaveOk
internal
replSetGetRBID: no-lock adminOnly slaveOk
internal
replSetGetStatus: no-lock adminOnly slaveOk
Report status of a replica set from the POV of this server
{ replSetGetStatus : 1 }
http://www.mongodb.org/display/DOCS/Replica+Set+Commands
replSetHeartbeat: no-lock
internal
replSetInitiate: no-lock adminOnly slaveOk
Initiate/christen a replica set.
http://www.mongodb.org/display/DOCS/Replica+Set+Commands
replSetReconfig: no-lock adminOnly slaveOk
Adjust configuration of a replica set
{ replSetReconfig : config_object }
http://www.mongodb.org/display/DOCS/Replica+Set+Commands
replSetStepDown: no-lock adminOnly slaveOk
Step down as primary. Will not try to reelect self or 1 minute.
(If another member with same priority takes over in the meantime, it will stay primary.)
http://www.mongodb.org/display/DOCS/Replica+Set+Commands
replSetTest: no-lock adminOnly slaveOk
Just for regression tests.
replacePeer: write-lock adminOnly slaveOk
replace a node in a replica pair
resetError: no-lock
reset error state (used with getpreverror)
resync: write-lock adminOnly slaveOk
resync (from scratch) an out of date replica slave.
http://www.mongodb.org/display/DOCS/Master+Slave
serverStatus: no-lock
returns lots of administrative server statistics
setShardVersion: write-lock adminOnly slaveOk
example: { setShardVersion : 'alleyinsider.foo' , version : 1 , configdb : '' }
shardingState: write-lock adminOnly slaveOk
no help defined
shutdown: write-lock adminOnly slaveOk
shutdown the database. must be ran against admin db and either (1) ran from localhost or (2) authenticated.
sleep: no-lock adminOnly slaveOk
internal testing command. Makes db block (in a read lock) for 100 seconds
w:true write lock
splitVector: read-lock
Internal command.
example: { splitVector : "myLargeCollection" , keyPattern : {x:1} , maxChunkSize : 200 }
maxChunkSize unit in MBs
NOTE: This command may take a while to run
top: read-lock adminOnly slaveOk
usage by collection
unsetSharding: no-lock adminOnly slaveOk
example: { unsetSharding : 1 }
validate: read-lock
Validate contents of a namespace by scanning its data structures for correctness. Slow.
whatsmyuri: no-lock
{whatsmyuri:1}
writebacklisten: no-lock adminOnly slaveOk
internal