mongodb之索引學習

來源:互聯網
上載者:User

標籤:search   article   

學習索引分類和建立索引:                
1._id索引 大多數集合預設的索引
2.單鍵索引:手動建立,一個單一的值
3.多建索引:組合函數
4.複合索引 :最左首碼原則
5.到期索引 :一定時間內失效,注意點:必須是isodate或者其數組,不要使用時間戳,否則不會被自動刪除。
6.全文索引 db.tm.ensureindex({"article":"text"}),db.tm.ensureindex({"key1":"text","key2":"text"}),db.tm.ensureindex({$**:"text"})
查詢:db.tm.find({$text:{$search:“aa”}})
db.tm.find({$text:{$search:"aa bb cc "}})
db.tm.find({$text:{$search:"aa bb -cc"}})
db.tm.find({$text:{$search:"\"a\"\"bb"\"cc\""}})
全文索引的匹配度$meta
db.tm.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}})
db.tm.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}})
每次只能指定$text
7.地理位置索引(滴滴打車,福士點評)


2d索引(地址位置索引)
db.localtion.ensureindex({"w":"2d"})

查看索引:db.tm.getIndexes()
建索引db.t1.ensureIndex({x:1})
多鍵索引db.tm.ensureIndex({x:[1,2,3,4,5]})
複合索引 db.tm.ensureIndex({x:1,y:1})
刪除索引db.tm.dropIndex("x_1_y_1")
db.tm.find({x:100,y:100}).explain()
到期索引:db.tm.insert({time:new Date()}) ISOdate 就是目前時間
db.tm.ensureIndex({time:1},{expireAfterSeconds:10})
db.tm.insert({time:new Date(),z:1})
全文索引
db.t1.ensureIndex({article:"text"})
db.t1.insert({article:"aa bb cc"})
尋找db.t1.find({$text:{$search:"aa"}})
db.t1.find({$text:{$search:"aa bb cc"}})或關係
db.t1.find({$text:{$search:"aa bb -cc"}})不包含CC
db.t1.find({$text:{$search:"\"aa\"\" bb\"\" -cc\""}})且的關係


全文索引的相似性:db.t1.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}})
db.t1.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}})



索引命名
db.t1.ensureIndex({x:1,y:2,z:2},{name:"xyz"})
db.t1.dropIndex("xyz")
建立唯一索引
db.t2.ensureIndex({m:1,n:1},{unique:true})

查看s索引存在某個欄位

db.abc.find({m:{$exists:true}})

建立2d索引:平面地理位置索引,位置表示方式,經緯度[經度(-180,180),緯度(-90,90)]
db.location.ensureIndex({"w":"2d"})
db.location.insert({w:[1,1]})
db.location.insert({w:[1,2]})
db.location.insert({w:[3,2]})
db.location.insert({w:[32,22]})
db.location.insert({w:[100,90]})
就近查詢
db.location.find({w:{$near:[1,1]}})

查詢
db.location.find({w:{$near:[1,1],$maxDistance:10}})

地址位置索引
geoNear
db.runCommand({geoNear:"location",near:[1,2],maxDistance:10,num:1})
db.stats

for(i=1;i<10000;i++)db.tt.insert({n:i})

查看運行狀態
/export/mongodb/bin/mongostat -h 192.168.1.70:22222
faults locked idx miss沒有使用索引值
qr|qw讀寫隊列


查看當前層級
db.getProfilingStatus()
0:profile為關閉,mongodb不會記錄任何操作
1配合slowms使用,mongodb會記錄任何超過slowms的操作
2會記錄任何記錄
修改層級profile
db.setProfilingLevel(0)
db.system.profile.find().sort({$natural:1}).limit(10)
查詢排序

            db.system.indexes.find().sort({$nature:1})

            

本文出自 “DBSpace” 部落格,請務必保留此出處http://dbspace.blog.51cto.com/6873717/1869918

mongodb之索引學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.