• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >MongoDB > 详解MongoDB管理命令

详解MongoDB管理命令

作者:Joans 字体:[增加 减小] 来源:互联网 时间:2017-05-11

Joans通过本文主要向大家介绍了mongodb详解,mongodb启动命令,mongodb命令,mongodb命令行,mongodb 重启命令等相关知识,希望本文的分享对您有所帮助

MongoDB是一个NoSQL数据库系统:一个数据库可以包含多个集合(Collection),每个集合对应于关系数据库中的表;而每个集合中可以存储一组由列标识的记录,列是可以自由定义的,非常灵活,由一组列标识的实体的集合对应于关系数据库表中的行。下面通过熟悉MongoDB的基本管理命令,来了解MongoDB提供的DBMS的基本功能和行为。 

MongoDB命令帮助系统 

在安装MongoDB后,启动服务器进程(mongod),可以通过在客户端命令mongo实现对MongoDB的管理和监控。看一下MongoDB的命令帮助系统:

 root@dev:~# mongo
 MongoDB shell version: ..
 connecting to: test
 > help
     db.help()          help on db methods
     db.mycoll.help()       help on collection methods
     rs.help()          help on replica set methods
     help connect         connecting to a db help
     help admin          administrative help
     help misc          misc things to know
     help mr           mapreduce help
     show dbs           show database names
     show collections       show collections in current database
     show users          show users in current database
     show profile         show most recent system.profile entries with time >= ms
     use <db_name>        set current database
     db.foo.find()        list objects in collection foo
     db.foo.find( { a : } )   list objects in foo where a == 
     it              result of the last line evaluated; use to further iterate
     DBQuery.shellBatchSize = x  set default number of items to display on shell
     exit             quit the mongo shell 
</div>

这是MongoDB最顶层的命令列表,主要告诉我们管理数据库相关的一些抽象的范畴:数据库操作帮助、集合操作帮助、管理帮助。如果你想了解数据库操作更详细的帮助命令,可以直接使用db.help(),如下所示:

 db.help()
 DB methods:
     db.addUser(username, password[, readOnly=false])
     db.auth(username, password)
     db.cloneDatabase(fromhost)
     db.commandHelp(name) returns the help for the command
     db.copyDatabase(fromdb, todb, fromhost)
     db.createCollection(name, { size : ..., capped : ..., max : ... } )
     db.currentOp() displays the current operation in the db
     db.dropDatabase()
     db.eval(func, args) run code server-side
     db.getCollection(cname) same as db['cname'] or db.cname
     db.getCollectionNames()
     db.getLastError() - just returns the err msg string
     db.getLastErrorObj() - return full status object
     db.getMongo() get the server connection object
     db.getMongo().setSlaveOk() allow this connection to read from the nonmaster member of a replica pair
     db.getName()
     db.getPrevError()
     db.getProfilingLevel() - deprecated
     db.getProfilingStatus() - returns if profiling is on and slow threshold 
     db.getReplicationInfo()
     db.getSiblingDB(name) get the db at the same server as this one
     db.isMaster() check replica primary status
     db.killOp(opid) kills the current operation in the db
     db.listCommands() lists all the db commands
     db.printCollectionStats()
     db.printReplicationInfo()
     db.printSlaveReplicationInfo()
     db.printShardingStatus()
     db.removeUser(username)
     db.repairDatabase()
     db.resetError()
     db.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : }
     db.serverStatus()
     db.setProfilingLevel(level,<slowms>) =off =slow =all
     db.shutdownServer()
     db.stats()
     db.version() current version of the server
     db.getMongo().setSlaveOk() allow queries on a replication slave server 
</div>

对数据库进行管理和操作的基本命令,可以从上面获取到。如果想要得到更多,而且每个命令的详细用法,可以使用上面列出的db.listCommands()查询。

另一个比较基础的是对指定数据库的集合进行操作、管理和监控,可以通过查询db.mycoll.help()获取到:

db.mycoll.help()
 DBCollection help
     db.mycoll.find().help() - show DBCursor help
     db.mycoll.count()
     db.mycoll.dataSize()
     db.mycoll.distinct( key ) - eg. db.mycoll.distinct( 'x' )
     db.mycoll.drop() drop the collection
     db.mycoll.dropIndex(name)
     db.mycoll.dropIndexes()
     db.mycoll.ensureIndex(keypattern[,options]) - options is an object with these possible fields: name, unique, dropDups
     db.mycoll.reIndex()
     db.mycoll.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
                            e.g. db.mycoll.find( {x:} , {name:, x:} )
     db.mycoll.find(...).count()
     db.mycoll.find(...).limit(n)
     db.mycoll.find(...).skip(n)
     db.mycoll.find(...).sort(...)
     db.mycoll.findOne([query])
     db.mycoll.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
     db.mycoll.getDB() get DB object associated with collection
     db.mycoll.getIndexes()
     db.mycoll.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
     db.mycoll.mapReduce( mapFunction , reduceFunction , <optional params> )
     db.mycoll.remove(query)
     db.mycoll.renameCollection( newName , <dropTarget> ) renames the collection.
     db.mycoll.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
     db.mycoll.save(obj)
     db.mycoll.stats()
     db.mycoll.storageSize() - includes free space allocated to this collection
     db.mycoll.totalIndexSize() - size in bytes of all the indexes
     db.mycoll.totalSize() - storage allocated for all data and indexes
     db.mycoll.update(query, object[, upsert_bool, multi_bool])
     db.mycoll.validate() - SLOW
     db.mycoll.getShardVersion() - only for use with sharding 
</div>

有关数据库和集合管理的相关命令,是最基础和最常用的,如集合查询、索引操作等。

基本命令及实例 

下面通过实际的例子来演示一些常见的命令:

(一)基本命令 

1、show dbs

显示当前数据库服务器上的数据库

2、use pagedb

 切换到指定数据库pagedb的上下文,可以在此上下文中管理pagedb数据库以及其中的集合等

3、show collections

显示数据库中所有的集合(collection)

4、db.serverStatus()  

查看数据库服务器的状态。示例如下所示:

{
     "host" : "dev",
     "version" : "..",
     "process" : "mongod",
     "uptime" : ,
     "uptimeEstimate" : ,
     "localTime" : ISODate("--T::.Z"),
     "globalLock" : {
         "totalTime" : ,
         "lockTime" : ,
         "ratio" : .,
         "currentQueue" : {
             "total" : ,
             "readers" : ,
             "writers" : 
         },
         "activeClients" : {
             "total" : ,
             "readers" : ,
             "writers" : 
         }
     },
     "mem" : {
         "bits" : ,
         "resident" : ,
         "virtual" : ,
         "supported" : true,
         "mapped" : 
     },
     "connections" : {
         "current" : ,
         "available" : 
     },
     "extra_info" : {
         "note" : "fields vary by platform",
         "heap_usage_bytes" : ,
         "page_faults" : 
     },
     "indexCounters" : {
         "btree" : {
             "accesses" : ,
             "hits" : ,
             "misses" : ,
             "resets" : ,
             "missRatio" : .
         }
     },
     "backgroundFlushing" : {
         "flushes" : ,
         "total_ms" : ,
         "average_ms" : .,
         "last_
  


 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • MongoDB 游标详解及实例代码
  • Mongodb如何开启用户访问控制详解
  • window平台安装MongoDB数据库图文详解
  • MongoDB 导出导入备份恢复数据详解及实例
  • 详解MongoDB中创建集合与删除集合的操作方法
  • 详解清除MongoDB所占用的多余的磁盘空间的方法
  • 详解MongoDB管理命令
  • MongoDB安全配置详解
  • MongoDB系列教程(七):MongoDb数据结构详解
  • mongodb 数据库操作详解--创建,切换,删除

相关文章

  • 2017-05-11MongoDB使用小结:一些不常见的经验分享
  • 2017-05-11MongoDB安装到windows服务的方法及遇到问题的完美解决方案
  • 2017-05-11MongoDB系列教程(八):GridFS存储详解
  • 2017-05-11mongoDB 实现主从读写分离实现的实例代码
  • 2017-05-11MongoDB中强大的统计框架Aggregation使用实例解析
  • 2017-05-11MongoDB入门教程之C#驱动操作实例
  • 2017-05-11MongoDB的安装方法图文教程
  • 2017-05-11mongodb replica set 添加删除节点的2种方法
  • 2017-05-11mongodb 修改器($inc/$set/$unset/$push/$pop/upsert)
  • 2017-08-25MongoDB安装为service报错100(windows系统)

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • 教大家8天学通MongoDB——第一天 基础入门篇
    • MongoDB 备份与恢复
    • MongoDB加入到Windows服务的方法
    • MongoDB的查询方法
    • MongoDB的基础知识简介
    • MongoDB查询操作限制返回字段的方法
    • 利用mongodb查询某坐标是否在规定多边形区域内的方法
    • MongoDB中对文档的增删查改基本操作方法总结
    • MongoDB的安装及配置文件选项全解
    • PHP中安装使用mongodb数据库

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有