细节
启动服务: -f 配置文件路径,-fork deamon方式启动
关闭服务:use admin -> db.shutdownServer()
cursor
var cursor = db.things.find();
1.while(cursor.hasNext()) {printjson(cursor.next());}
2.cursor.forEach(printjson);
3.cursor[0] (考虑内存溢出)
访问控制
绑定IP –bind_ip
设置监听端口 –port
使用用户名和口令登录 –auth–>db.createUser()–>db.auth();
进程
查看进程:db.currentOp();
干掉进程:db.killOp(opid);
索引(组合,唯一,文档)
创建: db.things.ensureIndex({age:1});
获取:db.things.getIndexes(); {backgroud:true}后台执行;{unique:true}唯一索引
强制使用:db.things.find({age:{$gt:20}}).hint((age:1})
查看索引是否使用:explain()
删除所有:db.things.dropIndexes()
删除指定:db.things.dropIndex({age:1})
性能优化
planA:创建索引,创建索引提高搜索,但以写入减慢为代价
planB:限定返回结果条数
planC:只查使用到的字段,而不查所有字段
planD:采用capped collection,capped collection大小固定,不可delete操作,只可drop;默认基于insert次序排序;FIFO,超过collection大小,新纪录替换最先insert的记录
planE:采用Server Side Code Execution(类似sql数据库的存储过程)
planF:Hint
planG:采用profiling
性能监控
mongosniff从底层监控那些命令发送到Mongodb执行
./mongosniff –source NET lo
mongostat快速查看某组运行中的Mongodb实例
./mongostat
db.serverStatus 查看实例运行状态
db.stats 查看数据库状态信息