MongoDB's avaliable commands

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介:
命令是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数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
相关文章
|
3月前
|
JSON NoSQL 小程序
Mongodb数据库的导出和导入总结
Mongodb数据库的导出和导入总结
195 0
|
3月前
|
存储 NoSQL 大数据
mongodb数据库的优缺点
MongoDB 是一个流行的 NoSQL 数据库,主要用于大规模数据存储和处理。下面是 MongoDB 数据库的一些优点和缺点: ### 优点: 1. **灵活的模式设计**:MongoDB 是一个文档数据库,支持动态的模式设计,允许您存储不同结构和格式的数据。 2. **水平扩展**:MongoDB 支持水平扩展,可以轻松地在多个节点和服务器之间分布数据,以满足高并发和大规模数据处理的需求。 3. **丰富的查询功能**:MongoDB 提供了强大的查询语言和索引支持,允许您在大数据集上进行高效的数据检索和分析。 4. **高性能**:通过使用内存映射(mmap)和其他优化技术,M
318 0
|
2天前
|
NoSQL MongoDB 数据库
MongoDB数据恢复—MongoDB数据库文件被破坏的数据恢复案例
服务器数据恢复环境: 一台Windows Server操作系统服务器,服务器上部署MongoDB数据库。 MongoDB数据库故障&检测: 工作人员在未关闭MongoDB数据库服务的情况下,将数据库文件拷贝到其他分区。拷贝完成后将原MongoDB数据库所在分区进行了格式化操作,然后将数据库文件拷回原分区,重新启动MongoDB服务,服务无法启动。
|
6天前
|
NoSQL MongoDB Redis
Python与NoSQL数据库(MongoDB、Redis等)面试问答
【4月更文挑战第16天】本文探讨了Python与NoSQL数据库(如MongoDB、Redis)在面试中的常见问题,包括连接与操作数据库、错误处理、高级特性和缓存策略。重点介绍了使用`pymongo`和`redis`库进行CRUD操作、异常捕获以及数据一致性管理。通过理解这些问题、易错点及避免策略,并结合代码示例,开发者能在面试中展现其技术实力和实践经验。
125 8
Python与NoSQL数据库(MongoDB、Redis等)面试问答
|
1月前
|
NoSQL 网络协议 MongoDB
Windows公网远程连接MongoDB数据库【无公网IP】
Windows公网远程连接MongoDB数据库【无公网IP】
|
1月前
|
存储 NoSQL 关系型数据库
一篇文章带你搞懂非关系型数据库MongoDB
一篇文章带你搞懂非关系型数据库MongoDB
57 0
|
1月前
|
人工智能 NoSQL MongoDB
|
2月前
|
SQL NoSQL Java
文档型数据库MongoDB
文档型数据库MongoDB
|
2月前
|
JSON NoSQL MongoDB
MongoDB详解(五)——MongoDB数据库简单使用
MongoDB详解(五)——MongoDB数据库简单使用
106 1
|
2月前
|
存储 NoSQL Linux
MongoDB详解(四)——MongoDB数据库安装
MongoDB详解(四)——MongoDB数据库安装
67 2