MongoDB的使用之前也分享過一篇,稍微高階點:見這里:《MongoDB使用小結》
1、shell登陸和顯示
假設在本機上有一個端口為17380的MongoDB服務,假設已經把mongo bin文件加入到系統PATH下。
登陸:mongo --port 17380
顯示DB:show dbs
進入某DB:use test_cswuyg
顯示集合:show tables
2、簡單查找
查找文檔:db.test_mac_id.find({'a': 'b'})
刪除文檔:db.test_mac_id.remove({'a': 'b'})
查找找到某一天的數據:
db.a.find({'D' : ISODate('2014-04-21T00:00:00Z')}) 或者 db.a.find({'D' : ISODate('2014-04-21')})
刪除某一天的數據:
db.region_mac_id_result.remove({"D" : ISODate('2014-04-17')})
小于2014.6.5的數據:
db.xxx.find({E: {$lt :ISODate('2014-06-05')}})
大于等于2014.6.1的數據:
db.xxx.find({E: {$gte: ISODate("2014-05-29")}}).count()
兩個條件:
db.xxx.find({E:{$gte: ISODate("2014-05-29"), $lte: ISODate("2014-06-04")}}).count()
json中的嵌套對象查詢,采用“點”的方式:
mongos> db.wyg.find({"a.b": {$exists: true}})
{ "_id" : "c", "a" : { "b" : 10 } }
某個字段存在,且小于1000有多少:
db.stat.find({_: ISODate("2014-06-17"), "123": {$exists: 1, $lte: 1000}}, {"123": 1}).count()
3、存在和遍歷統計
存在'i': 1,且存在old_id字段:
mongos> var it = db.test.find({'i': 1, "old_id": {$exists: 1}})
遍歷計數1:mongos> var count = 0;while(it.hasNext()){if (it.next()["X"].length==32)++count}print(count)
遍歷計數2:mongos> var count = 0;while(it.hasNext()){var item = it.next(); if (item['X'].length==32 && item['_id'] != item['X'])++count;if(!item['X'])++count;}print(count)
4、插入和更新
> db.test.findOne({_id: 'cswuyg'})
null
> db.test.insert({'_id': 'cswuyg', 'super_admin': true})
> db.test.findOne({'_id': 'cswuyg'})
{
"_id" : "cswuyg",
"super_admin" : true
}
db.test.update({'_id': 'cswuyg'}, {$set: {'super_admin': true}})
5、repair 操作
對某個DB執行repair:進入要repair的db,執行db.repairDatabase()
對mongodb整個實例執行repair:numactl --interleave=all /mongod --repair --dbpath=/home/disk1/mongodata/shard/
6、mongodb任務操作
停止某個操作:
[xxx]$ mongo --port 17380MongoDB shell version: 2.4.5connecting to: 127.0.0.1:17380/testmongos> db.currentOp(){ "inprog" : [ ...] }mongos> db.killOp("shard0001:163415563")
批量停止:
db.currentOp().inprog.forEach(function(item){db.killOp(item.opid)})
當查詢超過1000秒的,停止:
db.currentOp().inprog.forEach(function(item){if(item.secs_running > 1000 )db.killOp(item.opid)})
停止某個數據源的查詢:
db.currentOp().inprog.forEach(function(item){if(item.ns == "cswuyg.cswuyg")db.killOp(item.opid)})
把所有在等待鎖的操作顯示出來:
db.currentOp().inprog.forEach(function(item){if(item.waitingForLock)print(JSON.stringify(item))})
把處于等待中的分片顯示出來:
db.currentOp().inprog.forEach(function(item){if(item.waitingForLock){print(item.opid.substr(0,9));print(item.op);}})
把非等待的分片顯示出來:
db.currentOp().inprog.forEach(function(item){if(!item.waitingForLock){var lock_info = item["opid"];print(lock_info.substr(0,9));print(item["op"]);}})
查找所有的查詢任務:
db.currentOp().inprog.forEach(function(item){if(item.op=="query"){print(item.opid);}})
查找所有的非查詢任務:
db.currentOp().inprog.forEach(function(item){if(item.op!="query"){print(item.opid);}})
查找所有的操作:
db.currentOp().inprog.forEach(function(item){print(item.op, item.opid);});
常用js腳本,可直接復制到mongo-shell下使用:
顯示當前所有的任務狀態:
print("##########");db.currentOp().inprog.forEach(function(item){if(item.waitingForLock){var lock_info = item["opid"];print("waiting:",lock_info,item.op,item.ns);}});print("----");db.currentOp().inprog.forEach(function(item){if(!item.waitingForLock){var lock_info = item["opid"];print("doing",lock_info,item.op,item.ns);}});print("##########");
殺掉某些特定任務:
(1)
db.currentOp().inprog.forEach(function(item){if(item.waitingForLock){var lock_info = item["opid"];if(item.op=="query" && item.secs_running >60 && item.ns=="cswuyg.cswuyg"){db.killOp(item.opid)}}})
(2)
db.currentOp().inprog.forEach(function(item) { var lock_info = item["opid"]; if (item.op == "query" && item.secs_running > 1000) { print("kill", item.opid); db.killOp(item.opid) }})
7、刪除并返回數據
old_item = db.swuyg.findAndModify({query: {"_id": "aabbccdd"}, fields:{"D": 1,'E':1, 'F':1}, remove: true})
fields里面為1的是要返回的數據。
8、分布式集群部署情況
(1) 細致到collection的顯示:sh.status()
(2)僅顯示分片:
use config; db.shards.find()
{ "_id" : "shard0000", "host" : "xxhost:10001" }
{ "_id" : "shard0001", "host" : "yyhost:10002" }
....
(3)
use admin
db.runCommand({listshards: 1})
列出所有的shard server
9、正則表達式查找
正則表達式查詢:
mongos> db.a.find({"tt": /t*/i})
{ "_id" : ObjectId("54b21e0f570cb10de814f86b"), "aa" : "1", "tt" : "tt" }
其中i表明是否是case-insensitive,有i則表示忽略大小寫
db.testing.find({"name":/[7-9]/})
當name的值為789這幾個數字組成的字符串時,查詢命中。
10、查詢性能
db.testing.find({name: 123}).explain()
輸出結果:
{ "cursor" : "BasicCursor", "isMultiKey" : false, "n" : 1, "nscannedObjects" : 10, "nscanned" : 10, "nscannedObjectsAllPlans" : 10, "nscannedAllPlans" : 10, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 0, "nChunkSkips" : 0, "millis" : 0, "indexBounds" : { }, "server" : "xxx:10001"}
11、更新或插入
當該key不存在的時候執行插入操作,當存在的時候則不管,可以使用setOnInsert
db.wyg.update({'_id': 'id'}, {'$setOnInsert': {'a': 'a'}, '$set': {'b': 'b'}}, true)
當id存在的時候,忽略setOnInsert。
當id存在的時候,如果要插入,則插入{'a': 'a'}
最后的參數true,則是指明,當update不存在的_id時,執行插入操作。默認是false,只更新,不插入。
push、setOnInsert:db.cswuyg.update({"_id": "abc"}, {$push: {"name": "c"}, $setOnInsert: {"cc":"xx"}}, true)
12、計算DB中collection的數量
db.system.namespaces.count()
13、增加數字,采用$inc
db.cswuyg.update({"a.b": {$exists: true}}, {$inc: {'a.b': 2}})
也就是對象a.b的值,增加了2
注意$inc只能用于數值。
14、刪除某個key
db.cswuyg.update({'_id': 'c'}, {$unset: {'b': {$exists: true}}})
{ "_id" : "c", "a" : { "b" : 12 }, "b" : 7 }
轉變為:
{ "_id" : "c", "a" : { "b" : 12 } }
15、增加key:value
db.cswuyg.update({'_id': 'z'}, {'$set': {'hello': 'z'}})
{ "_id" : "z", "b" : 1 }
轉變為:
{ "_id" : "z", "b" : 1, "hello" : "z" }
16、刪除數據庫、刪除表
刪除數據庫:db.dropDatabase();
刪除表:db.mytable.drop();
17、查找到數據只看某列
只顯示key名為D的數據:db.test.find({}, {D: 1})
18、查看分片存儲情況
(1)所有DB的分片存儲信息,包括chunks數、shard key信息:db.printShardingStatus()
(2)db.collection.getShardDistribution() 獲取collection各個分片的數據存儲情況
(3)sh.status() 顯示本mongos集群所有DB的信息, 包含了Shard Key信息
19、查看collection的索引
db.cswuyg.getIndexes()
20、開啟某collection的分片功能
1. ./bin/mongo
主站蜘蛛池模板:
av最新在线观看
|
国产精品久久久久久久娇妻
|
4480午夜|
久久久日韩精品一区二区
|
久久精精
|
91a在线观看
|
国产一级片91
|
91九色论坛
|
国产精品视频一区二区噜噜
|
成人午夜视频免费
|
免费在线观看毛片
|
九九热国产在线
|
免费黄色大片网站
|
av色哟哟
|
鲁人人人鲁人人鲁精品
|
一级一级一级一级毛片
|
国产精品久久久久久久亚洲按摩
|
精品国产一区二区久久
|
成人免费网站在线观看
|
欧美黄色大片免费观看
|
国产精品看片
|
久久中文字幕在线观看
|
艹男人的日日夜夜
|
黄色片网站免费在线观看
|
国产精品视频久久久
|
精品久久久久久久久久久下田
|
精品国产一区二区三区四区在线
|
www国产网站
|
毛片在线免费播放
|
免费欧美一级视频
|
国产正在播放
|
国产成人午夜高潮毛片
|
李宗瑞国产福利视频一区
|
精品国产一级毛片
|
婷婷中文字幕一区二区三区
|
精品国产一区二区在线观看
|
久久久久免费电影
|
一级免费特黄视频
|
欧美一区二区三区久久精品视
|
孕妇体内谢精满日本电影
|
香蕉国产9
|