1:升序;-1:降序
db.user.ensureIndex({name:1},{options}) //Deprecated
db.user.createIndex({name:1},{options}) //options: background:true;unique:true
db.user.getIndexes()
db.user.dropIndex("index_name")
db.user.dropIndexes()
--master
--slave --source <master:port>
如果不指定source,可以登录slave,然后
db.sources.insert({host:"<master:port>"})
db.sources.find();
效果同source参数
----ReplSet
config={_id:"",members:[{_id:"",host:""}....]}
rs.initiate(config)
rs.reconfig(config,{force:true}) {force:true}可选 ,可能导致数据回滚, 执行命令时,必须连接一个Primary节点;
添加replset时,只需在Primary中执行:rs.add(host:port);
rs.slaveOk() 在secondary中执行操作;
rs.addArb(host:port) 添加仲裁者;决策哪台服务器是Primary,哪台是Secondary, 默认情况下mongodb会自动分配,可当如果是2个的话,就无法决策。
db.user.find({<condition>},{<field:0?1>}) //0:不显示,1:显示
=======================================================================
数据聚合:
db.user.aggregation({})
http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/
WHERE $match
GROUP BY$group
HAVING $match
SELECT $project
ORDER BY$sort
LIMIT $limit
SUM() $sum
COUNT() $sum
db.runCommand({
mapReduce:<collection>,
map:function,
finialize:function,
query:document,
sort:documnt,
limit:number,
scope:document
.....
})
http://docs.mongodb.org/manual/reference/command/mapReduce/
db.user.find({field:{$exists:true}}) //查询只有field的数据,false相反
============================================================================
存储过程
mongodb的存储过程在db.system.js
db.system.js.insert({_id:"<produceName>",value:<function(params){}>})
db.eval('<productName(params)>')
=============================================================================
导入导出
mongoexport -d <database> -c <collections> -o <filename> --csv -f <field> -h <host> -p <port> -u <user>
mongoimport -d <database> -c <collections> -o <filename> --type csv --headerline
mongodump -d <database> -o <filepath>
mongorestore -d <database> -c <collections> dumpfilepath
========================================================
监控
db.currentOp()
db.killOp(<pid>)
===========================================
db.createCollection("name",{capped:true,size:10000})
=====================================================================================
mkdir /data/test/config/data -p
mkdir /data/test/shard1/data -p
mkdir /data/test/shard2/data -p
mkdir /data/test/shard3/data -p
mkdir /data/test/config/log -p
mkdir /data/test/shard1/log -p
mkdir /data/test/shard2/log -p
mkdir /data/test/shard3/log -p
mkdir /data/test/mongos/log -p
---Config-----
mongod --configsvr --dbpath /data/test/config/data/ --port 21000 --logpath /data/test/config/log/config.log --fork
---Mongos-----
mongos --configdb 192.168.137.128:21000,192.168.137.129:21000,192.168.137.130:21000 --port 20000 --logpath /data/test/mongos/log/mongos.log --fork
mongod --shardsvr --replSet shard3 --port 22003 --dbpath /data/test/shard3/data/ --logpath /data/test/shard3/log/shard1.log --fork --oplogSize 10 --nojournal
mongod --shardsvr --replSet shard1 --port 22001 --dbpath /data/test/shard1/data/ --logpath /data/test/shard1/log/shard1.log --fork --oplogSize 10 --nojournal
mongod --shardsvr --replSet shard2 --port 22002 --dbpath /data/test/shard2/data/ --logpath /data/test/shard2/log/shard1.log --fork --oplogSize 10 --nojournal
---Replset------
mongos --port 22001
use admin
config={_id:shard1,members:[{_id:0,host:"shard1/192.168.137.128:22001,192.168.137.139:22001,192.168.137.130:22001"}]}
rs.initiate(config)
mongos --port 22002
use admin
config={_id:shard2,members:[{_id:1,host:"shard2/192.168.137.128:22002,192.168.137.139:22002,192.168.137.130:22002"}]}
rs.initiate(config)
mongos --port 22003
use admin
config={_id:shard3,members:[{_id:2,host:"shard3/192.168.137.128:22003,192.168.137.139:22003,192.168.137.130:22003"}]}
rs.initiate(config)
--------Shard-----
mongo --port 20000
use admin
db.runnCommand({addshard:"shard1/192.168.137.128:22001,192.168.137.129:22001,192.168.137.130:22001"})
db.runnCommand({addshard:"shard2/192.168.137.128:22002,192.168.137.129:22002,192.168.137.130:22002"})
db.runnCommand({addshard:"shard3/192.168.137.128:22003,192.168.137.129:22003,192.168.137.130:22003"})
db.runCommand({removeshard:"127.0.0.1:22001"})
db.runCommand({enablesharding:"testdb"}) //开启分片功能
db.runCommand({shardcollection:"testdb.user",key:{id:1}}) //指定分片的Key
db.printShardingStatus()
db.getName() //当前数据库名称
db.hostInfo() //系统信息
db.isMaster() //判断是否Master
db.runCommand({flushRouterConfig:1}) //清除mongos缓存的集群信息,重新加载configdb的集群信息;
isdbgrid:1 //判断当前链接的是不是mongos;如果不是会提示错误信息;
db.user.find().pretty(); //pretty(),将结果格式化输出
=====================================================================================