Mongodb的索引--學習筆記(未完)

來源:互聯網
上載者:User

標籤:

全文索引

建立方法:--在articles集合的key欄位上建立全文索引db.articles.ensureIndex({key:"text"})--在articles集合的key_1,key_2欄位上建立全文索引db.articles.ensuereIndex({key_1:"text",key_2:"text"})--在articles集合的所有欄位上建立全文索引db.articles.ensuereIndex({"$**":"text"})--在articles集合中的article欄位上建立全文索引> db.articles.ensureIndex({"article":"text"});{        "createdCollectionAutomatically" : true,        "numIndexesBefore" : 1,        "numIndexesAfter" : 2,        "ok" : 1}> --插入示範資料db.articles.insert({"article":"aa bb"})db.articles.insert({"article":"aa bb cc "})db.articles.insert({"article":"aa bb cc dd"})db.articles.insert({"article":"aa bb cc rr"})使用全文索引查詢--查詢包含aa 或 bb 或 cc的文檔db.articles.find({$text:{$search:"aa bb cc"}})--查詢包含aa 或 bb 但不包含 cc的文檔db.articles.find({$text:{$search:"aa bb -cc"}})--查詢包含aa 、bb 且包含cc的文檔db.articles.find({$text:{$search:"\"aa\" \"bb\" \"cc\""}})全文索引的相似性$meta操作符:{score:{$meta:"textScore"}}寫在查詢條件後面可以返回查詢結果的相似性經常與sort一起使用--查詢含有"aa bb"欄位的文本,並返回查詢結果的相似性> db.articles.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}});{ "_id" : ObjectId("5798ad223206da9bc38b2370"), "article" : "aa bb cc dd", "score" : 1.25 }{ "_id" : ObjectId("5798ad293206da9bc38b2371"), "article" : "aa bb cc rr", "score" : 1.25 }{ "_id" : ObjectId("5798ad1b3206da9bc38b236f"), "article" : "aa bb cc ", "score" : 1.3333333333333333 }{ "_id" : ObjectId("5798ad143206da9bc38b236e"), "article" : "aa bb", "score" : 1.5 }{ "_id" : ObjectId("5798ae383206da9bc38b2372"), "article" : "aa", "score" : 1.1 }--根據相似性排序> db.articles.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}});{ "_id" : ObjectId("5798ad143206da9bc38b236e"), "article" : "aa bb", "score" : 1.5 }{ "_id" : ObjectId("5798ad1b3206da9bc38b236f"), "article" : "aa bb cc ", "score" : 1.3333333333333333 }{ "_id" : ObjectId("5798ad223206da9bc38b2370"), "article" : "aa bb cc dd", "score" : 1.25 }{ "_id" : ObjectId("5798ad293206da9bc38b2371"), "article" : "aa bb cc rr", "score" : 1.25 }{ "_id" : ObjectId("5798ae383206da9bc38b2372"), "article" : "aa", "score" : 1.1 }> 全文索引使用的限制(版本2.6.5)1.每次查詢,只能指定一個$text查詢(一個集合只能建立一個全文索引)2.$text查詢不能出現在$nor查詢中3.查詢中如果包含$text,hint不在起作用4.當前Mongodb的全文索引還不支援中文

索引屬性

建立索引的格式:db.collection.ensureIndex({indexValue},{indexProperty})其中indexProperty比較重要的有:1.名字,name指定:  db.collection.ensureIndex({},{name:""})2.唯一性,unique指定:  db.collection.ensureIndex({},{unique:true/false})3.稀疏性,sparse指定  稀疏性指是否為文檔中不存在的欄位建立索引  db.collection.ensureIndex({},{sparse:true/false})4.是否定時刪除,expireAfterSeconds指定:  TTL 到期索引

地理位置索引

地理位置索引    將一些點的位置儲存到Mongodb中,建立索引後,可以按照位置來尋找其他點子分類2d索引:用於儲存和尋找平面上的點2dsphere索引:用於儲存和尋找球面上的點尋找方式1.尋找距離某個點一定距離內的點2.尋找包含在某地區內的點2d索引詳解1.建立索引方式  db.collection.ensureIndex({w:"2d"})2.位置表示方式  經緯度[經度,緯度]3.取值範圍  經度[-180,180] 緯度[-90,90]4.插入位置資料  >db.location.insert({w:[1,1]})  >db.location.insert({w:[1,2]})  >db.location.insert({w:[5,6]})  >db.location.insert({w:[200,1]}) #Mongodb會直接報錯,經度超出範圍  >db.location.insert({w:[180,100]}) #緯度超出範圍,Mongodb並沒有報錯,但是後期查詢會出現不可預知的錯誤。  >db.location.insert({w:[79,76]})5.查詢方式    $near查詢 查詢距離某個點的最近點    >db.location.find({w:{$near:[1,1]}}) #預設返回100個    --可通過$maxDistance來限制尋找的最遠距離    >db.location.find({w:{$near:[1,1],$maxDistance:10}}) #限制尋找最遠距離為10    $geoWithin查詢 查詢某個形狀內的點    形狀有三種表示方式    1)$box:矩形,使用{$box:[[<x1>,<y1>],[<x2>,<y2>]]}表示,內部是兩個座標,第一個代表左邊界,第二個代表右邊界。    >db.collection.find({w:{$geoWithin:{$box:[[0,0],[3,3]]}}})    2)$center:圓形,使用{$center:[[<x1>,<y1>],r]}表示,內部是圓心位置和半徑。    >db.collection.find({w:{$geoWithin:{$center:[[0,0],5]}}})    3)$polygon:多邊形,使用{$polygon:[[<x1>,<y1>],[<x2>,<y2>],[<x3>,<y3>]]}表示,內部是座標點,座標點圍成一個多邊形。    >db.collection.find({w:{$geoWithin:{$polygon:[[0,0],[0,1],[2,5],[6,1]]}}})  geoNear查詢 是對$near查詢的補充  db.runCommand({         getNear:<collection>,         near:[x,y],         minDistance:(對2d索引無效)         maxDistance:         num:     })

 

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.