1. 程式人生 > >MongoDB使用小結:一些常用操作分享

MongoDB使用小結:一些常用操作分享

hist b數 文件 存儲信息 無法拷貝 分享 rand case accep

本文整理了一年多以來我常用的MongoDB操作,涉及mongo-shell、pymongo,既有運維層面也有應用層面,內容有淺有深,這也就是我從零到熟練的歷程。

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 17380
MongoDB shell version: 2.4.5
connecting to: 127.0.0.1:17380/test
mongos> 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 –port 20000

2. mongos> use admin

3. switched to db admin

4. mongos> db.runCommand({‘enablesharding"‘ ‘test‘})

5. { "ok" : 1 }

開啟user collection分片功能:

1. mongos> db.runCommand({‘shardcollection‘: ‘test.user‘, ‘key‘: {‘_id‘: 1}})

{ "collectionsharded" : "test.user", "ok" : 1 }

21、判斷當前是否是shard集群

isdbgrid:用來確認當前是否是 Sharding Cluster

> db.runCommand({isdbgrid:1});
是:
{ "isdbgrid" : 1, "hostname" : "xxxhost", "ok" : 1 }
不是:
{
        "ok" : 0,
        "errmsg" : "no such cmd: isdbgrid",
        "code" : 59,
        "bad cmd" : {
                "isdbgrid" : 1
        }
}

22、$addToSet、$each插入數組數據

mongos> db.cswuyg.find()

{ "_id" : "wyg", "a" : "c", "add" : [ "a", "b" ] }

mongos> db.cswuyg.update({"_id": "wyg"}, {"$set": {"a": "c"}, "$addToSet": {"add": {"$each" :["a", "c"]}}}, true)

mongos> db.cswuyg.find()

{ "_id" : "wyg", "a" : "c", "add" : [ "a", "b", "c" ] }

$each是為了實現list中的每個元素都插入,如果沒有$each,則會把整個list作為一個元素插入,變成了2維數組。

$addToSet會判斷集合是否需要排重,保證集合不重。$push可以對數組添加元素,但它只是直接插入數據,不做排重。

eg:db.test.update({"a": 1}, {$push: {"name": {$each:["a", "c"]}}})
eg:db.test.update({"a": 1}, {$addToSet: {"name": {$each: ["a", "d"]}}})

不去重插入 pushAll

> db.push.insert({"a": "b", "c": ["c", "d"]})

> db.push.find()

{ "_id" : ObjectId("53e4be775fdf37629312b96c"), "a" : "b", "c" : [ "c", "d" ]

}

> db.push.update({"a":"b"}, {$pushAll:{"c": ["z", "d"]}})

> db.push.find()

{ "_id" : ObjectId("53e4be775fdf37629312b96c"), "a" : "b", "c" : [ "c", "d",

"z", "d" ] }

pushAll跟push類似,不同的是pushAll可以一次插入多個value,而不需要使用$each。

23、刷新配置信息

db.runCommand("flushRouterConfig");

24、批量更新

db.xxx.update({"_id": {$exists: 1}}, {$set: {"_" : ISODate("2014-03-21T00: 00:00Z")}}, true, true)

最後一個參數表示是否要批量更新,如果不指定,則一次只更新一個document。

25、dump DB

mongodump支持從DB磁盤文件、運行中的MongoD服務中dump出bson數據文件。

(1)關閉MongoD之後,從它的DB磁盤文件dump出數據(註:僅限單實例mongod):

mongodump --dbpath=/home/disk1/mongodata/shard/ -d cswuyg -o /home/disk2/mongodata/shard

參考:

http://docs.mongodb.org/manual/reference/program/mongodump/

http://stackoverflow.com/questions/5191186/how-do-i-dump-data-for-a-given-date

(2)從運行的MongoD中導出指定日期數據,采用-q查詢參數:

mongodump -h xxxhost --port 17380 --db cswuyg --collection test -q "{D: {\$gte: {\$date: `date -d "20140410" +%s`000}, \$lt: {\$date: `date +%s`000}}}"

mongodump -dbpath=/home/disk3/mongodb/data/shard1/ -d cswuyg -c test -o /home/disk9/mongodata/shard1_2/ -q "{_:{\$gte:{\$date:`date -d "20140916" +%s`000}, \$lt: {\$date: `date -d "20140918" +%s`000}}}"

dump出來的bson文件去掉了索引、碎片空間,所有相比DB磁盤文件要小很多。

26、restore DB

restore的時候,不能先建索引,必須是restore完數據之後再建索引,否則restore的時候會非常慢。而且一般不需要自己手動建索引,在數據bson文件的同目錄下有一個索引bson文件(system.indexes.bson),restore完數據之後會mongorestore自動根據該文件創建索引。

(1)從某個文件restore

mongorestore --host xxxhost --port 17380 --db cswuyg --collection cswuyg ./cswuyg_1406330331/cswuyg/cswuyg_bak.bson

(2)從目錄restore

./mongorestore --port 27018 /home/disk2/mongodata/shard/

(3)dump 和 restore configure server:

mongodump --host xxxhost --port 19913 --db config -o /home/work/cswuyg/test/config

mongorestore --host xxxhost --port 19914 /home/work/cswuyg/test/config

27、創建索引

看看當前的test collection上有啥索引:
mongos> db.test.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "cswuyg.test", "name" : "_id_" } ]
當前只有_id這個默認索引,我要在test collection上為 index 字段創建索引: mongos> db.test.ensureIndex({"index": 1})
創建完了之後,再看看test collection上的索引有哪些: mongos> db.test.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "ns" : "cswuyg.test", "name" : "_id_" }, { "v" : 1, "key" : { "index" : 1 }, "ns" : "cswuyg.test", "name" : "index_1" } ]

創建索引,並指定過期時間:db.a.ensureIndex({‘_‘:-1}, {expireAfterSeconds: 1000}) 1000Second.

修改過期時間: db.runCommand({"collMod": "a", index: {keyPattern:{"_": -1}, expireAfterSeconds: 60}})

28、刪除索引

db.post.dropIndexes() 刪除post上所有索引

db.post.dropIndex({name: 1}) 刪除指定的單個索引

29、唯一索引問題

如果集群在_id上進行了分片,則無法再在其他字段上建立唯一索引:

mongos> db.a.ensureIndex({‘b‘: 1}, {‘unique‘: true})
{
        "raw" : {
                "set_a/xxxhost:20001,yyyhost:20002" : {
                        "createdCollectionAutomatically" : false,
                        "numIndexesBefore" : 1,
                        "ok" : 0,
                        "errmsg" : "cannot create unique index over { b: 1.0 } with shard key pattern { _id: 1.0 }",
                        "code" : 67
                },
                "set_b/xxxhost:30001,yyyhost:30002" : {
                        "createdCollectionAutomatically" : false,
                        "numIndexesBefore" : 1,
                        "ok" : 0,
                        "errmsg" : "cannot create unique index over { b: 1.0 } with shard key pattern { _id: 1.0 }",
                        "code" : 67
                }
        },
        "code" : 67,
        "ok" : 0,
        "errmsg" : "{ set_a/xxxhost:20001,yyyhos:20002: \"cannot create unique index over { b: 1.0 } with shard key pattern { _id: 1.0 }\", set_b/xxxhost:30001,yyyhost:30002: \"cannot create unique index over { b: 1.0 } with shard key pattern { _id: 1.0 }\" }"
}

之所以出現這個錯誤是因為MongoDB無法保證集群中除了片鍵以外其他字段的唯一性,能保證片鍵的唯一性是因為文檔根據片鍵進行切分,一個特定的文檔只屬於一個分片,MongoDB只要保證它在那個分片上唯一就在整個集群中唯一,實現分片集群上的文檔唯一性一種方法是在創建片鍵的時候指定它的唯一性。

30、因遷移導致出現count得到的數字是真實數字的兩倍

cswuyg> db.test.find({D: ISODate(‘2015-08-31‘), B: ‘active‘}).count()

52118

cswuyg> db.test.find({D: ISODate(‘2015-08-31‘), B: ‘active‘}).forEach(function(item){db.test_count.insert(item)})

cswuyg> db.test_count.count()

26445

解決方法:http://docs.mongodb.org/manual/reference/command/count/#behavior

“On a sharded cluster, count can result in an inaccurate count if orphaned documents exist or if a chunk migration is in progress.

To avoid these situations, on a sharded cluster, use the $group stage of the db.collection.aggregate()method to $sum the documents. ”

31、自定義MongoDB操作函數

可以把自己寫的js代碼保存在某個地方,讓MongoDB加載它,然後就可以在MongoDB的命令行裏操作它們。

mongodb shell默認會加載~/.mongorc.js文件

例如以下修改了啟動提示文字、左側提示文字,增加了my_show_shards shell函數用於顯示當前sharded collection的chunks在各分片的負載情況:

//~/.mongorc.js
//show at begin var compliment = ["attractive", "intelligent", "like batman"]; var index = Math.floor(Math.random()*3); print("Hello, you‘re looking particularly " + compliment[index] + " today!"); //change the prompt prompt = function(){ if (typeof db == "undefined") { return "(nodb)> "; } // Check the last db operation try { db.runCommand({getLastError: 1}); } catch (e) { print(e); } return db + "> "; } //show all shard‘s chunks function my_show_shards() { var config_db = db.getSiblingDB("config"); var collections = {}; var shards = {}; var shard_it = config_db.chunks.find().snapshot(); while (shard_it.hasNext()) { next_item = shard_it.next(); collections[JSON.stringify(next_item["ns"]).replace(/\"/g, "")] = 1; shards[JSON.stringify(next_item["shard"]).replace(/\"/g, "")] = 1; } var list_collections = []; var list_shards = []; for (item in collections) { list_collections.push(item); } for (item in shards) { list_shards.push(item); } list_collections.forEach(function(collec) { list_shards.forEach(function(item) { obj = {}; obj["shard"] = item; obj["ns"] = collec; it = config_db.chunks.find(obj); print(collec, item, it.count()); }) }) }

32、關閉mongod

mongo admin --port 17380 --eval "db.shutdownServer()"

33、查看chunks信息

eg: 進入到config db下,執行

db.chunks.find()

34、預分片參考

http://blog.zawodny.com/2011/03/06/mongodb-pre-splitting-for-faster-data-loading-and-importing/

35、DB重命名

db.copyDatabase("xx_config_20141113", "xx_config")

use xx_config_20141113

db.dropDatabase();

遠程拷貝DB :

db.copyDatabase(fromdb, todb, fromhost, username, password)

db.copyDatabase("t_config", "t_config_v1", "xxxhost: 17380")

這個拷貝過程很慢。

註意,sharded的DB是無法拷貝的,所以sharded的DB也無法采用上面的方式重命名。

參考: http://docs.objectrocket.com/features.html "Your remote database is sharded through mongos."

拷貝collection:

db.collection.copyTo("newcollection")

同樣,sharded的collection也無法拷貝。

36、聚合運算

包括:

1、pipeline;

2、map-reduce

管道:

http://docs.mongodb.org/manual/tutorial/aggregation-with-user-preference-data/

http://docs.mongodb.org/manual/reference/operator/aggregation/#aggregation-expression-operators

http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.aggregate

2.6之前的MongoDB,管道不支持超過16MB的返回集合。

可以使用$out操作符,把結果寫入到collection中。如果aggregation成功,$out會替換已有的colleciton,但不會修改索引信息,如果失敗,則什麽都不做。

http://docs.mongodb.org/manual/reference/operator/aggregation/out/#pipe._S_out

37、aggregate pipeline demo

demo1,基礎:

cswuyg_test> db.cswuyg_test.aggregate({"$match": {"_": ISODate("2015-02-16"), "$or": [{"13098765": {"$exists": true}}, {"13123456": {"$exists": true}}]}}, {"$group": {"_id": "$_", "13098765": {"$sum": "$13098765"}, "13123456":{"$sum": "$13123456"}}})

demo2,使用磁盤:

db.test.aggregate([{‘$match‘: {‘D‘: {‘$nin‘: [‘a‘, ‘b‘, ‘c‘]}, ‘_‘: {‘$lte‘: ISODate("2015-02-27"), ‘$gte‘: ISODate("2015-02-26")}}}, {‘$group‘: {‘_id‘: {‘a‘: ‘$a‘, ‘b‘: ‘$b‘, ‘D‘: ‘$D‘}, ‘value‘: {‘$sum‘: 1}}}], {‘allowDiskUse‘: true})

demo3,指定輸出文檔:

db.cswuyg.aggregate([ {‘$match‘: {‘D‘: {‘$nin‘: [‘a‘, ‘b‘, ‘c‘]}, ‘_‘: {‘$lte‘: ISODate("2015-02-10"), ‘$gte‘: ISODate("2015-02-09")}}}, {‘$group‘: {‘_id‘: {‘C‘: ‘$C‘, ‘D‘: ‘$D‘}, ‘value‘: {‘$sum‘: 1}}}, {"$out": "cswuyg_out"}], {‘allowDiskUse‘:true})
db.b.aggregate([{$match: {a: {$size: 6}}}, {$group: {_id: {a: ‘$a‘}}}, {$out: ‘hh_col‘}], {allowDiskUse: true})

註:指定輸出文檔,只能輸出到本DB下。

aggregate練習:

pymongo代碼:

[{‘$match‘: {u‘I‘: {u‘$in‘: [XXX‘]}, u‘H‘: u‘id‘, u‘12345678‘: {u‘$exists‘: True}, ‘_‘:{‘$lte‘: datetime.datetime(2015, 6, 1, 23, 59, 59), ‘$gte‘: datetime.datetime(2015, 6, 1, 0, 0)}}}, {‘$group‘: {‘_id‘: {u‘12345678‘: u‘$12345678‘, u‘G‘: u‘$G‘}, ‘value‘: {‘$sum‘: 1}}}]

shell下代碼:

db.test.aggregate([{$match: {_:ISODate("2015-06-01"), "H": "id", "12345678": {"$exists": true}, "I": "XXX"}}, {$group: {_id: {"12345678": "$12345678", "G": "$G"}, "value": {"$sum": 1}}}], {allowDiskUse:true})

38、修改Key:Value中的Value

給字段B的值加上大括號‘{‘:

db.test.find({_:ISODate("2014-11-02")}).forEach(function(item){if(/{.+}/.test(item["B"])){}else{print(item["B"]);db.test.update({"_id": item["_id"]}, {"$set": {"B": "{" + item["B"] + "}"}})}})

39、修改primary shard

db.runCommand({"movePrimary": "test", "to": "shard0000"})

這樣子test DB 裏的非sharded cocllection數據就會被存放到shard0000中,非空DB的話,可能會有一個migrate的過程。

40、 mongodb默認開啟autobalancer

balancer是sharded集群的負載均衡工具,新建集群的時候默認開啟,除非你在config裏把它關閉掉:

config> db.settings.find()

{ "_id" : "chunksize", "value" : 64 }

{ "_id" : "balancer", "activeWindow" : { "start" : "14:00", "stop" : "19:30" }, "stopped" : false}

activeWindow指定autobalancer執行均衡的時間窗口。

stopped說明是否使用autobalancer。

手動啟動balancer:sh.startBalancer()

判斷當前balancer是否在跑:sh.isBalancerRunning()

41、MongoDB插入性能優化

插入性能:200W的數據,在之前沒有排序就直接插入,耗時4小時多,現在,做了排序,插入只需要5分鐘。排序對於單機版本的MongoDB性能更佳,避免了隨機插入引發的頻繁隨機IO。

排序:在做分文件排序的時候,文件分得越小,排序越快,當然也不能小到1,否則頻繁打開文件也耗費時間。

42、MongoDB數組操作

1、更新/插入數據,不考慮重復值:

mongos> db.test.update({"helo":"he2"}, {"$push": {"name":"b"}})

多次插入後結果:

{ "_id" : ObjectId("54a7aa2be53662aebc28585f"), "helo" : "he2", "name" : [ "a", "b", "b" ] }

2、更新/插入數據,保證不重復:

mongos> db.test.update({"helo":"she"}, {"$addToSet": {"name":"b"}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })

多次插入後結果:

{ "_id" : ObjectId("54dd6e1b570cb10de814f86d"), "helo" : "she", "name" : [ "b" ] }

保證只有一個值。

3、數組元素個數:

$size 用來指定數組的元素個數,顯示fruit數組長度為3的document:

mongos> db.a.find({"fruit": {$size: 3}})

{ "_id" : ObjectId("54b334798220cd3ad74db314"), "fruit" : [ "apple", "orange", "cherry" ] }

43、更換Key: Value中的Key

verRelease 換為 TEST

db.test.find().forEach(function(item){db.test.update({_id:item["_id"]}, {"$set": {"TEST": item["verRelease"]}}, {"$unset":{"verRelease":{"exists":true}}})})

或者更新部分修改為:

db.test.update({D : ISODate("2014-11-02")}, {$rename : {"AcT": "AM"}}, false, true)

44、手動在shell下moveChunk

config> sh.moveChunk("xx.yy", { "_id" : { "D" : ISODate("2015-02-24T00:00:00Z"), "id" : "3f" } }, "shard0003")

如果出現錯誤,參考這裏:可能需要重啟

http://stackoverflow.com/questions/26640861/movechunk-failed-to-engage-to-shard-in-the-data-transfer-cant-accept-new-chunk

45、MongoDB升級後的兼容問題

MongoDB 2.4切換到2.6之後,出現數據沒有插入,pymongo代碼:

update_obj = {"$set" : {"a": 1}}

update_obj["$inc"] = inc_objs

update_obj["$push"] = list_objs

db.test.update({"_id": id}, update_obj, True)

2.6裏,inc如果是空的,則會導致整條日誌沒有插入,2.4則inc為空也可以正常運行。

46、格式化json顯示

db.collection.find().pretty()

47、更新replica集群的域名信息

cfg = rs.conf()
cfg.members[0].host = "xxxhost: 20000"
cfg.members[1].host = "yyyhost: 20001"
cfg.members[2].host = "zzzhost: 20002"
rs.reconfig(cfg)

48、不要直接修改local.system.replset

不要直接修改local.system.replset,因為他只能修改本機器的本地信息。

但是如果出現了:

 {
        "errmsg" : "exception: can‘t use localhost in repl set member names except when using it for all members",
        "code" : 13393,
        "ok" : 0
 }

這樣的錯誤,那就要修改本機的local.system.replset,然後重啟。

49、排重統計

(1)aggregate

result = db.flu_test.aggregate([{$match: {_: ISODate("2015-05-01")}}, {$group:{_id:"$F", value: {$sum:1}}}], {allowDiskUse:true})

result.itcount()

(2)distinct

flu_test> db.flu_test.distinct(‘F‘, {_:ISODate("2015-06-22")}) 但是,由於distinct將結果保存在list中,所以很容易觸發文檔超過16MB的錯誤: 2015-06-23T15:31:34.479+0800 distinct failed: { "errmsg" : "exception: distinct too big, 16mb cap", "code" : 17217, "ok" : 0 } at src/mongo/shell/collection.js:1108

非排重文檔量統計:

mongos> count = db.flu_test.aggregate([{$match:{_:ISODate("2015-05-21")}}, {$group:{_id:null, value: {$sum:1}}}], {allowDiskUse:true})

{ "_id" : null, "value" : 3338987 }

50、pymongo優先讀取副本集Secondary節點

優先讀取副本集Secondary節點,可以減少primary節點負擔,在primary節點跟secondary節點同步延遲較短、業務對數據不要求實時一致時可以利用副本集做讀寫分離和負載均衡。

副本集集群的讀取有這幾種使用方式:

primary: 默認參數,只從主節點讀取;
primaryPreferred: 大部分從主節點上讀取,主節點不可用時從Secondary節點讀取;
secondary: 只從Secondary節點上進行讀取操作;
secondaryPreferred: 優先從Secondary節點讀取,Secondary節點不可用時從主節點讀取;
nearest: 從網絡延遲最低的節點上讀取。

(1)測試1 優先從secondary讀取數據:

import pymongo


client = pymongo.MongoReplicaSetClient(‘xxxhost: yyyport‘,  replicaSet=‘my_set‘, readPreference=‘secondaryPreferred‘)
print client.read_preference  # 顯示當前的讀取設定
for i in xrange(1, 10000):  # 循環10000次,用mongostat觀察查詢負載
    a = client[‘history‘][‘20140409‘].find_one({"ver_code": "128"})
    print a

(2)測試2 直接連接某Secondary節點讀取數據:

import pymongo


client = pymongo.MongoClient(‘xxxhost‘, yyyport, slaveOk=True)
a = client[‘msgdc_ip‘][‘query_ip‘].find().count()
print a

參考:

http://www.lanceyan.com/category/tech/mongodb
http://emptysqua.re/blog/reading-from-mongodb-replica-sets-with-pymongo/

http://api.mongodb.org/python/current/api/pymongo/read_preferences.html#module-pymongo.read_preferences

http://api.mongodb.org/python/current/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient

註意:3.0之後MongoReplicaSetClient函數是要被放棄的。

但是測試時發現:在較低版本中,需要使用MongoReplicaSetClient,MongoClient無法實現 pymongo.ReadPreference.SECONDARY_PREFERRED功能。

2015.12.28補充:

51、為副本集設置標簽

可以為副本集中的每個成員設置tag(標簽),設置標簽的好處是後面讀數據時,應用可以指定從某類標簽的副本上讀數據。

為replica 設置tag
在副本shell下執行: var conf = rs.conf() conf.members[0].tags = { "location": "beijing" } conf.members[1].tags = { "location": "hangzhou"} conf.members[2].tags = { "location": "guangzhou" } rs.reconfig(conf)

參考:https://docs.mongodb.org/v3.0/tutorial/configure-replica-set-tag-sets/

52、副本集碎片整理的一種方法

使用MMAPv1存儲引擎時,對於頻繁大數據量的寫入刪除操作,碎片問題會變得很嚴重。在數據同步耗時不嚴重的情況下,我們不需要對每個副本做repair,而是輪流“卸下副本,刪除對應的磁盤文件,重新掛上副本”。每個重新掛上的副本都會自動去重新同步一遍數據,碎片問題就解決了。

53、存儲引擎升級為wiredTiger

我們當前的版本是MongoDB3.0.6,沒有開啟wiredTiger引擎,現在打算升級到wiredTiger引擎。

我們是Shared Cluster,shard是Replica Set。升級比較簡單,只需要逐步對每一個副本都執行存儲引擎升級即可,不影響線上服務。

升級時,只在啟動命令中添加:--storageEngine wiredTiger。

步驟:首先,下掉一個副本;然後,把副本的磁盤文件刪除掉;接著,在該副本的啟動命令中添加--storageEngine wiredTiger後啟動。這就升級完一個副本,等副本數據同步完成之後,其它副本也照樣操作(或者處理完一個副本之後,拷貝該副本磁盤文件替換掉另一個副本的磁盤文件)。風險:如果數據量巨大,且有建索引的需求,容易出現內存用盡。

分片集群的configure server可以不升級。

升級之後磁盤存儲優化效果極度明顯,22GB的數據會被精簡到1.3GB。

升級後的磁盤文件完全變了,所以不同存儲引擎下的磁盤文件不能混用。

升級參考:https://docs.mongodb.org/manual/tutorial/change-sharded-cluster-wiredtiger/

54、oplogSizeMB不要設置得太大

啟動配置中的這個字段是為了設置oplog collection的大小,oplog是操作記錄,它是一個capped collection,在副本集群中,設置得太小可能導致secondary無法及時從primary同步數據。默認情況下是磁盤大小的5%。但是,如果這個字段設置得太大,可能導致暴內存,oplog的數據幾乎是完全加載在內存中,一旦太大,必然暴內存,導致OOM。而且因為這個collection是capped,MongoDB啟動之後無法修改其大小。

MongoDB使用小結:一些常用操作分享