首页 > 技术文章 > mongodb常用操作

chenjw-note 2019-05-31 12:13 原文

1.无认证启动mongodb

numactl --interleave=all /usr/local/mongodb/bin/mongod --port %d --master --dbpath %s --logpath %s --pidfilepath %s --logappend  --noauth --oplogSize=240 --fork

2.创建mongodb认证

host=$MONGODB_HOST:$MONGODB_PORT
mongo $host/admin --eval "db.system.version.remove({});"
mongo $host/admin --eval "db.system.version.insert({'_id':'authSchema','currentVersion':3});"
mongo $host/admin --eval "db.createRole({role:'sysadmin',roles:[],privileges:[{resource:{anyResource:true},actions:['anyAction']}]});"
mongo $host/admin --eval "db.createUser({user:'$MONGODB_USER',pwd:'$MONGODB_PASS',roles:['sysadmin']})"

3.启动认证mongodb

numactl --interleave=all /usr/local/mongodb/bin/mongod --port %d --master --dbpath %s --logpath %s --pidfilepath %s --logappend --auth  --keyFile=mongodb_key --oplogSize=240 --fork

4.做mongodb从库

numactl --interleave=all /usr/local/mongodb/bin/mongod --source 主库ip:主库端口 --port %d --slave --dbpath %s --logpath %s --pidfilepath %s --logappend --auth --keyFile=mongodb_key  --nohttpinterface --autoresync  --fork

5.外部执行mongodb语句

echo 'db.gl.scene.drop();' | /usr/local/mongodb/bin/mongo 127.0.0.1:27317/sid${server_id} -uxxx -pxxxx --authenticationMechanism=MONGODB-CR --authenticationDatabase=admin

6.导出某条件的数据【此处不开启认证】

/usr/local/mongodb/bin/mongoexport -h ${source_ip} --port ${source_port} -d sid${server_id} -c gl.player_secretary -q {'pi':${pi}} -o gl.player_secretary_${pi}.json

7.删除某条件的数据

echo 'db.gl.player_secretary.remove({\"pi\":${pi}});' | /usr/local/mongodb/bin/mongo 127.0.0.1:27317/sid${server_id} -uxxx -pxxxx --authenticationMechanism=MONGODB-CR --authenticationDatabase=admin

8.导入json数据

/usr/local/mongodb/bin/mongoimport -h 127.0.0.1 --port=27317  -uxxx -pxxxx --authenticationMechanism=MONGODB-CR --authenticationDatabase=admin -d sid${server_id} -c gl.player_secretary --upsert gl.player_secretary_${pi}.json

 9.常用查询

查询pid小于10000000的玩家:db.PlayerInfo.find({pid:{$lt:10000000}});
删除pid小于10000000的玩家:db.PlayerInfo.remove({pid:{$lt:10000000}});
查询mongodb最大值:db.gl.player.find().sort({pi:-1}).skip(0).limit(3);
按条件查找并统计: db.PlayerInfo.find({"pid":{$lt:1000000}}).count();

10.更新操作

按条件更新:db.gl.player.update({"pi" : 571473931},{$set:{"na" : "狼王玛丽aaaaa"}});  #前面{}为条件,$set为更新的内容 更新多个字段,直接在$set:{}里面加就好了

 11.查找集合【gl.arena_ranking_list】中有重复的rank,显示重复的rank和重复的个数

db.getCollection('gl.arena_ranking_list').aggregate([{ $group: { "_id" : '$rank', count: { $sum : 1 } } },{ $match: { count: { $gt : 1} } }])

 12.导出指定表的数据

mongoexport -h127.0.0.1 --port 27317 -uxxx -pxxxx --authenticationMechanism=MONGODB-CR --authenticationDatabase=admin -d bbh_15 -c arenabots -o arenabots.json

 13.导入指定表的数据

/usr/local/mongodb/bin/mongoimport -h127.0.0.1 --port 27317 -uxxx -pxxxx --authenticationMechanism=MONGODB-CR --authenticationDatabase=admin -d bbh_21 -c arenabots arenabots.json

 14.查询某字段最大/最小值的一条数据

最大:db.表名.find().sort({字段:-1}).skip(0).limit(1);
最小:db.表名.find().sort({字段:1}).skip(0).limit(1);

 15.查询某一字段值

db.resources.find({"playerId":13636030},{"level":1, _id:0})

1表示显示此字段
0表示不显示此字段
默认会显示_id,其他字段自己随便加

16.查询某一字段的所有值

db.players.find({},{"playerId":1,_id:0})

playerId:是要查的字段

17.查询美观输出

db.shareadvertises.find().pretty()

18.创建指定用户指定库

db.createUser({user: "rwuser",pwd: "bbh123123",roles: [{ role: "readWrite", db: "tkot2_53" }]})

 

推荐阅读