一、mongodb/bin下的命令

MongoDB在linux上的基本操作

1.登录

mongo
MongoDB在linux上的基本操作

2.mongo状态检测

mongostat是mongdb自带的状态检测工具,在命令行下使用。
间隔固定时间获取mongodb的当前运行状态,并输出。
如果你发现数据库突然变慢或者有其他问题的话,你第一手的操作就考虑采用mongostat来查看mongo的状态。

MongoDB在linux上的基本操作

参数 说明
insert 每秒插入次数
query 每秒查询次数
update 每秒更新次数
delete 每秒删除次数
getmore 每秒执行getmore次数
command 每秒的命令数,比以上插入、查找、更新、删除的综合还多,还统计了别的命令
dirty
used
flushes 每秒执行fsync将数据写入硬盘的次数。
vsize 虚拟内存使用量,单位MB
res 物理内存使用量,单位MB
qr qw
ar aw
netIn
netOut
conn 当前连接数
time 时间戳

二、数据库操作

数据库的帮助文档

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()                   // 返回当前程序的版本信息

1.查看数据库版本

db.version()
MongoDB在linux上的基本操作

2.查看数据库状态

db.stats()
MongoDB在linux上的基本操作

参数 说明
db 当前数据库
collection 集合的个数
objects 对象的个数, 循环每个集合得到的,每个集合的记录数(nrecords)之和
avgObjSize 平均每个对象的大小, 通过 dataSize / objects 得到
dataSize 每个集合的dataSize之和
storageSize 每个集合的storageSize之和
indexes 索引个数
indexSize 每个索引数据和
fileSize db下面的物理存储文件的大小; 物理存储文件比实际的数据要大一些,这是是mongo的预分配机制

3.查看所有数据库

show dbs

4.查看当前数据库

db
db.getName()

5.切换数据库

use rec

6.查看当前数据库的所有集合

show collections

三、集合操作

集合的帮助文档

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()                  // 返回数据集共享版本号  

1.查询

db.interface_log.find()
MongoDB在linux上的基本操作

相关文章: