mongodb查看數據庫和表的方法比較簡單,在爲這裏推薦使用stats的方法,直觀而且詳細。mongodb
一、查看數據庫
db.stats();
1
輸出:shell
{
"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
}
二、查看數據庫表
db.sirius.stats();
1
輸出:數據庫
{
"ns" : "sirius.sirius",
"size" : 84,
"count" : 2,
"avgObjSize" : 42,
"storageSize" : 36864,
"capped" : false,
"wiredTiger" : {...},
"nindexes" : 1,
"totalIndexSize" : 36864,
"indexSizes" : {
"_id_" : 36864
},
"ok" : 1
}
三、db的幫助文檔
db.help();
1
輸出:服務器
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.cname
db.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 commands
db.loadServerScripts() // loads all the scripts in db.system.js
db.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 db
db.unsetWriteConcern( <write concern doc> ) // unsets the write concern for writes to the db
db.setVerboseShell(flag) // display extra information in shell output
db.shutdownServer() // 關閉當前服務程序
db.stats() // 返回當前數據庫的狀態信息
db.version() // 返回當前程序的版本信息app
四、表的幫助
db.tableName.help();
1
輸出:ide
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() // 返回數據集共享版本號
--------------------- this