./mongo 127.0.0.1:37017/mydb -uadmin -pTran --authenticationDatabase admin --eval "printjson(db.asset_scan.findOne());printjson(db.asset_scan.findOne());"
./mongo 127.0.0.1:37017/mydb -uadmin -pTran --authenticationDatabase admin --eval "db.currentOp()"
./mongo 127.0.0.1:37017 test.js
./mongo --nodb test.js 不用默认的连接就必须使用 --nodb选项参数
test.js var url = "mongodb://admin:Tran@127.0.0.1:37017/" var con = new Mongo(url) var db = con.getDB("mydb") printjson(db.stats())test.js
设置分片
use admin
db.auth("admin","admin")
db.runCommand({enablesharding:"mydb"}) //必须先切换到admin数据库然后才能有权限创建分区表
对新表进行设置
db.runCommand({shardcollection:"mydb.asset_acceptance_info",key:{_id:'hashed'}})
对已经有数据的表设置
db.runCommand({shardcollection:"mydb.asset_acceptance_info",key:{"_id":1}})
var url = "mongodb://admin:Tran@127.0.0.1:37017/"
var con = new Mongo(url)
var db = con.getDB("mydb")
db.adminCommand({shardcollection:"mydb.test7",key:{"_id":1}})
printjson(db.test7.getShardDistribution())
var url = "mongodb://admin:Tran@192.168.0.103:37017/" var con = new Mongo(url) var db = con.getDB("mydb") //创建分区表名称为test8 db.adminCommand({shardcollection:"mydb.test8",key:{"_id":1}}) //创建分区表的索引字段 db.test8.ensureIndex({"_id":1}) db.test8.ensureIndex({"ipv4":1}) printjson(db.test8.getShardDistribution()) printjson(db.test8.stats())View Code