《mongodb入門》讀書筆記下載:
http://download.csdn.net/detail/ch717828/9833847
MongoDB的索引
- 查看索引
db.test_table.getIndexes()
- 創建索引(1表示升序,-1表示降序)
db.test_table.ensureIndex({x:1})
索引的種類
_id索引
_id索引是絕大多數集合默認建立的索引,對於每個插入數據MongoDB都會自動生成一條唯一的_id字段
單鍵索引
單鍵是最普通的索引
db.test_table.ensureIndex({x:1})
多鍵索引
在單鍵索引的基礎上,多鍵索引的值有多個記錄,例如數組
db.test_table.insert({x:[1,2,3,4,5]})
對於這條插入記錄來講,mongodb即爲x創建了一個多鍵索引
複合索引
使用多個鍵作爲索引
db.test_table.ensureIndex({x:1,y:1})
過期索引
在一段時間後會過期的索引,在索引過期後,相應的數據會被刪除。 這適合存儲一些在一段時間之後會失效的數據比如用戶的登陸信息、存儲的日誌
db.test_table.ensureIndex({y:1},{expireAfterSeconds:30}) db.test_table.insert({y:new Date()}) db.test_table.insert({y:1})
觀察一段時間後會發現,y的值爲 new Date()的記錄被自動刪除,y的值爲1的值沒有被刪除。因此
存儲在過期索引字段的值必須是指定的時間類型(ISODate或者ISODate數組,不能使用時間戳)。如果制定了ISODate數組,則按照數組中的最小的時間進行刪除。此外,過期索引不能是複合索引。刪除時間不是精確的(刪除過程由後臺程序定時執行,而且刪除過程也需要時間,因此存在誤差)全文索引
創建全文索引(每個數據集合只允許創建一個全文索引)
db.test_table.ensureIndex({key:"text"}) db.test_table.ensureIndex({key_1:"text",key_2:"text"}) db.test_table.ensureIndex({"$**":"text"}) #$**表示在所有的字符串字段上創建一個全文索引。
使用全文索引
db.test_table.insert({"article":"abcd abcd abcd"}) db.test_table.insert({"article":"11 22 33"}) db.test_table.ensureIndex({"article":"text"}) db.test_table.find({$test:{$search:"abcd"}}) db.test_table.find({$test:{$search:"abcd 11"}}) db.test_table.find({$test:{search:"abcd -11"}}) #包含abcd但不包含11 db.test_table.find({$test:{$serach:"\"abcd\" \"11\" "}}) #即包含abcd又包含11 db.test_table.find({$test:{$search:"abcd 1234"}},{score:{$meta:"textScore"}}) #全文索引相似度
地理位置索引
2D平面地理位置索引
db.test_location.ensureIndex({"w":"2d"}) #創建2D平面地理位置索引 db.test_location.insert({w:[100,150]}) #插入記錄 db.test_location.find({w:{$near:[1,1]}}) #查找距離[1,1]最近的點(默認返回前100個) db.test_location.find({w:{$near:[1,1],$maxDistance:10}}) #查找距離[1,1]距離不超過10的點 db.test_location.find({w:{$geoWithin:{$box:[[0,0],[3,3]]}}}) #查找矩形[ [0,0],[3,3] ] 內的點 db.test_location.find({w:{$geoWithin:{$center:[[0,0],5]}}}) #查找圓心[0,0]半徑爲5的圓內的點 db.test_location.find({w:{$geoWithin:{$polygon:[[0,0],[0,1],[2,2],[3,3]]}}}) #查找多邊形[[0,0],[0,1],[2,2],[3,3]]內的點 db.runCommand({geoNear:"test_location",near:[1,2],$maxDistance:10,num:2}) #查找test_location中,距離[1,2]最大距離不超過10的2條記錄
2D球面地理位置索引
索引的名字
db.test_table.ensureIndex({x:1,y:1,z:1},{name:"normal})
索引的唯一性
db.test_table.ensureIndex({m:1,n:1},{unique:true}) db.test_table.insert({m:1,n:2}) #插入成功 db.test_table.insert({m:1,n:2}) #插入失敗,鍵衝突
索引的稀疏性(稀疏性爲true表示不爲不存在的字段創建索引)
db.test_table.ensureIndex({x:1},{sparse:true})
不可以在稀疏索引上查找不存在的記錄,例:
“`
db.test_table.insert({m:1})
db.test_table.insert({n:1})
db.test_table.find({m:{exists:true}}) #查找m存在的記錄
db.test_table.ensureIndex({m:1},{sparse:true}) #創建稀疏索引
db.test_table.find({m:{ exists:false}}) #查找m不存在的記錄,依然找出m,這是稀疏索引的問題
db.test_table.find({m:{$exists:false}}).hind(“m_1”) #可以實現查找m不存在的記錄“`
索引構建情況分析
mongostat: 查看mongodb運行狀態的程序
./bin/mongostat --hlep #查看mongostat幫助 ./bin/mongostat -h 127.0.0.1:12345 #查看當前系統的運行情況(如查看每秒有多少寫入)
explain: 顯示一次查詢的詳細信息
db.test_table.find({x:1}).explain();
MongoDB安全
開啓MongoDB的鑑權
vim conf/mongod.confport = 12345 dbpath = data logpath = log/mongod.log fork = true author = true ``` 使用createUser創建用戶
db.createUser({user:”testUser”,pwd:”testUser”,roles:[{role:”userAdmin”,db:”admin”},{role:”read”,db:”test”}]}) #創建testUser用戶,對admin有userAdmin權限,對test有read權限
“`
問題
- 多鍵索引的作用
多鍵索引與單鍵索引在使用方式上有很大區別,在單鍵索引的基礎上,若插入的值爲數組,則MongoDB爲其創建一個多鍵索引。查詢時,使用多鍵數組中的任意一個值均可以找到該條記錄。