mongodb查看数据库和表的信息

来源:互联网 发布:手机淘宝卖家怎么激活 编辑:程序博客网 时间:2024/05/21 19:47

mongodb查看数据库和表的方法比较简单,在为这里推荐使用stats的方法,直观并且详细。

1、查看数据库

    db.stats();

输出:

{    "db" : "sirius",    "collections" : 3,    "objects" : 5,    "avgObjSize" : 55.2,    "dataSize" : 276,    "storageSize" : 16384,    "numExtents" : 3,    "indexes" : 1,    "indexSize" : 8176,    "fileSize" : 50331648,    "nsSizeMB" : 16,    "ok" : 1}

2、查看数据库表

 db.sirius.stats();

输出:

{    "ns" : "sirius.sirius",    "size" : 84,    "count" : 2,    "avgObjSize" : 42,    "storageSize" : 36864,    "capped" : false,    "wiredTiger" : {...},    "nindexes" : 1,    "totalIndexSize" : 36864,    "indexSizes" : {        "_id_" : 36864    },    "ok" : 1}

3、db的帮助文档

db.help();

输出:

db.adminCommand(nameOrDocument)// 切换到'admin'数据库,并且运行命令db.AddUser(username,password[, readOnly=false])  //添加用户  db.auth(usrename,password)     // 设置数据库连接验证  db.cloneDataBase(fromhost)     // 从目标服务器克隆一个数据库  db.commandHelp(name)           // returns the help for the command  db.copyDatabase(fromdb,todb,fromhost)  // 复制数据库fromdb---源数据库名称,todb---目标数据库名称,fromhost---源数据库服务器地址  db.createCollection(name,{size:3333,capped:333,max:88888})  // 创建一个数据集,相当于一个表  db.createView(name, viewOn, [ { $operator: {...}}, ... ], { viewOptions } ) // 创建视图db.createUser(userDocument)    // 创建用户db.currentOp()                 // 取消当前库的当前操作  db.dropDataBase()              // 删除当前数据库  db.eval(func,args)             // (已过时) run code server-side  db.fsyncLock()                 // 将数据保存到硬盘并且锁定服务器备份db.fsyncUnlock() unlocks server following a db.fsyncLock()db.getCollection(cname)        // 取得一个数据集合,同用法:db['cname'] or db.cnamedb.getCollenctionNames()       // 取得所有数据集合的名称列表  db.getLastError()              // 返回最后一个错误的提示消息  db.getLastErrorObj()           // 返回最后一个错误的对象  db.getLogComponents()db.getMongo()                  // 取得当前服务器的连接对象get the server  db.getMondo().setSlaveOk()     // allow this connection to read from then nonmaster membr of a replica pair  db.getName()                   // 返回当操作数据库的名称  db.getPrevError()              // 返回上一个错误对象  db.getProfilingLevel()         // 获取profile level  db.getReplicationInfo()        // 获得重复的数据  db.getSisterDB(name)           // get the db at the same server as this onew  db.killOp()                    // 停止(杀死)在当前库的当前操作 db.listCommands()              // lists all the db commandsdb.loadServerScripts()         // loads all the scripts in db.system.jsdb.logout()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:1}  db.runCommand(cmdObj)          // run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }db.serverStatus()db.setLogLevel(level, <component>)db.setProfilingLevel(level, <slowms>)    // 设置profile level 0=off,1=slow,2=all db.setWriteConcern( <write concern doc> ) // sets the write concern for writes to the dbdb.unsetWriteConcern( <write concern doc> ) // unsets the write concern for writes to the dbdb.setVerboseShell(flag)       // display extra information in shell outputdb.shutdownServer()            // 关闭当前服务程序  db.stats()                     // 返回当前数据库的状态信息db.version()                   // 返回当前程序的版本信息

4、表的帮助

db.tableName.help();

输出:

db.test.find({id:10})          // 返回test数据集ID=10的数据集  db.test.find({id:10}).count()  // 返回test数据集ID=10的数据总数  db.test.find({id:10}).limit(2) // 返回test数据集ID=10的数据集从第二条开始的数据集  db.test.find({id:10}).skip(8)  // 返回test数据集ID=10的数据集从0到第八条的数据集  db.test.find({id:10}).limit(2).skip(8)  // 返回test数据集ID=1=的数据集从第二条到第八条的数据  db.test.find({id:10}).sort()   // 返回test数据集ID=10的排序数据集  db.test.findOne([query])       // 返回符合条件的一条数据  db.test.getDB()                // 返回此数据集所属的数据库名称  db.test.getIndexes()           // 返回些数据集的索引信息  db.test.group({key:...,initial:...,reduce:...[,cond:...]})    // 返回分组信息  db.test.mapReduce(mayFunction,reduceFunction,<optional params>)  // 这个有点像存储过程  db.test.remove(query)                      // 在数据集中删除一条数据  db.test.renameCollection(newName)          // 重命名些数据集名称  db.test.save(obj)                          // 往数据集中插入一条数据  db.test.stats()                            // 返回此数据集的状态  db.test.storageSize()                      // 返回此数据集的存储大小  db.test.totalIndexSize()                   // 返回此数据集的索引文件大小  db.test.totalSize()                        // 返回些数据集的总大小  db.test.update(query,object[,upsert_bool]) // 在此数据集中更新一条数据  db.test.validate()                         // 验证此数据集  db.test.getShardVersion()                  // 返回数据集共享版本号  

参考文章

一路博客(http://www.16boke.com)

0 0