1宪郊、_id索引: ? ?
自動(dòng)創(chuàng)建
2浦楣、單鍵索引:
【值為一個(gè)單個(gè)的值狞悲,例如字符串汁汗、數(shù)字或者日期】
db.nums.insert({x:1,y:2,z:3})
db.nums.ensureIndex({x:1})
說(shuō)明:索引的key是某個(gè)具體的字段衷畦,value為固定值1(升序)或-1(倒序)
3、多建索引:
【值具有多個(gè)紀(jì)錄知牌,例如數(shù)組祈争,與單建索引創(chuàng)建的形式一樣,區(qū)別在于字段的值】
db.nums.insert({x:['a','b','c','d']})
db.nums.ensureIndex({x:1})
4角寸、復(fù)合索引:
db.nums.insert({x:1,y:2,z:3})
db.nums.ensureIndex({x:1,y:-1})
說(shuō)明:對(duì)多個(gè)字段加索引
5菩混、過(guò)期索引(ttl索引):
在一段時(shí)間之后會(huì)過(guò)期的索引
在索引過(guò)期后,相應(yīng)的數(shù)據(jù)會(huì)被刪除
這適合存儲(chǔ)一些在一段時(shí)間之后會(huì)失效的數(shù)據(jù)扁藕,比如用戶的登錄信息沮峡、存儲(chǔ)日記
限制:
(1)存儲(chǔ)在過(guò)期索引字段的值必須是指定的時(shí)間類型
說(shuō)明:必須是ISODate或者ISODate數(shù)組,不能是時(shí)間戳亿柑,否則不能被自動(dòng)刪除
(2)如果指定了ISODate數(shù)組邢疙,則按照最小的時(shí)間進(jìn)行刪除
(3)過(guò)期索引不能是復(fù)合索引
(4)刪除時(shí)間不是精確的
說(shuō)明:刪除過(guò)程是由后臺(tái)程序每60秒跑一次,而且刪除也需要一些時(shí)間望薄,所以存在誤差
db.nums.ensureIndex({time:1},{expireAfterSeconds:10})
db.nums.insert({time:new Date()})
6疟游、全文索引(文本索引):
對(duì)字符串與字符串?dāng)?shù)組創(chuàng)建全文可搜索的索引
創(chuàng)建方法:固定值為text
db.article.ensureIndex({ key : "text" })【單個(gè)字段】
db.article.ensureIndex({ key_1 : "text" , key_2 : "text"})【多個(gè)字段】
db.article.ensureIndex({ "$**" : "text" })【表示對(duì)集合中所有字段創(chuàng)建一個(gè)大的全文索引】
db.article.ensureIndex({ "article" : "text" })
db.article.insert({"article":" aa bb cc dd ee"})
db.article.insert({"article":" aa bb rr gg"})
db.article.insert({"article":" aa bb cc hh jvjbvb"})
db.article.find({$text:{$search:"aa"}})
db.article.find({$text:{$search:"rr"}})
db.article.find({$text:{$search:"aa bb cc"}})【或查詢】
db.article.find({$text:{$search:"aa bb -cc"}})【不包含cc的】
db.article.find({$text:{$search:"\"aa\" \"bb\" \"cc\""}})【與查詢,用引號(hào)包含起來(lái)】
db.article.find({$text:{$search:"\"aa\" \"bb\" cc"}})
【每個(gè)數(shù)據(jù)集合只允許創(chuàng)建一個(gè)全文索引】
全文索引相似度
$meta操作符:{score:{$meta:"textScore"}}
寫(xiě)在查詢條件后面可以返回結(jié)果的相似度
與sore一起使用痕支,可以達(dá)到很好的實(shí)用效果
db.article.insert({"article":" aa bb"})
db.article.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}})
db.article.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}})
全文索引使用限制
每次查詢只能制定一個(gè)$text查詢
$text查詢不能出現(xiàn)在$nor查詢中
查詢中如果包含了$text,hint不再起作用
MongoDB全文索引還不支持中文
7颁虐、地理位置索引:
將一些點(diǎn)的位置存儲(chǔ)在MongoDB中,創(chuàng)建索引后卧须,可以按照位置來(lái)查找其他點(diǎn)
子分類:
? ? ? ? ? ? ?2d索引另绩,用于存儲(chǔ)和查找平面上的點(diǎn)【平面地理位置索引】
? ? ? ? ? ? ?2dsphere索引,用于存儲(chǔ)和查找球面上的點(diǎn)【球面地理位置索引】
查找方式:
? ? ? ? ? ? 1.查找距離某個(gè)點(diǎn)一定距離內(nèi)的點(diǎn)
? ? ? ? ? ? ?2.查找包含在某區(qū)域內(nèi)的點(diǎn)
2d索引詳解:
創(chuàng)建方式:db.article.ensureIndex({ "w" : "2d" })
位置表示方式:經(jīng)緯度[經(jīng)度,緯度]
取值范圍:經(jīng)度[-180,180],緯度[-90,90]
查詢方式:
(1) $near查詢:? ? 查詢距離某個(gè)點(diǎn)最近的點(diǎn)
db.location.find({w:{$near:[1,1]}})【默認(rèn)返回100個(gè)最近的點(diǎn)】
db.location.find({w:{$near: [1,1],$maxDistance: 10,$minDistance: 2}})【限制距離我最遠(yuǎn)的是10故慈,最近的是2】
3.X版本支持$minDiatance板熊,之前版本不支持
(2) $geoWithin查詢:查詢某個(gè)形狀內(nèi)的點(diǎn)
$box:矩形{$box:[[x1,y1],[x2,y2]]}
db.location.find({"w":{$geoWithin:{$box:[[0,0],[6,6]]}}})
$center:圓形{$center:[[x1,y1],r]}
db.location.find({"w":{$geoWithin:{$center:[[0,0],6]}}})
$polygon:多邊形{$polygon:[[x1,y1],[x2,y2],[x3,y3]]}
db.location.find({"w":{$geoWithin:{$polygon:[[0,0],[0,1],[2,5],[16,12]]}}})
(3) geoNear查詢
它是$geoWithin的進(jìn)化版,geoNear使用runCommand命令進(jìn)行使用
db.runCommand({
? ? ? geoNear:集合名,
? ? ? ?near:[x,y],
? ? ? ?maxDistance:
? ? ? ?minDistance:【對(duì)2d索引無(wú)效】
? ? ? ?num:限制返回的數(shù)目
})
db.runCommand({
? ? ? ? ?geoNear:"location",
? ? ? ? ?near:[4,3],
? ? ? ? ?maxDistance:10,
? ? ? ? ?num:5
})
2dsphere索引:球面地理位置索引【支持minDistance察绷,$maxDistance】
db.collection.ensureIndex({w:"2dsphere"})
位置表示方式:
GeoJSON:描述一個(gè)點(diǎn)干签,一條線,多邊形等形狀
格式:{ type: 'GeoJSON type' , coordinates: 'coordinates' }
其中type指的是類型拆撼,可以是Point(本例中用的)容劳,LineString喘沿,Polygon等,coordinates是一個(gè)坐標(biāo)數(shù)組竭贩。
比如:db.sphere.insert({name:"A",sp:{type:"Point",coordinates:[105.754484701156,41.689607057699]}})
可參考文章:http://blog.csdn.net/u014230597/article/details/52635190
db.sphere.insert({name:"A",sp:{type:"Point",coordinates:[105.754484701156,41.689607057699]}})
索引屬性:
名字-name指定:創(chuàng)建索引時(shí)指定索引名蚜印,刪除索引時(shí)指定索引名刪除
db.article.ensureIndex({ "article" : 1 } , { "name" : "index_article"})
db.article.dropIndex("index_article")
唯一性-unique指定:
db.article.ensureIndex({ "man_id_id" : 1 , "women_id_id" : 1 } , { "unique" : true})
稀疏性-sparse指定:
db.article.ensureIndex({ "manss_id" : 1 } , { "sparse" : true/false})【默認(rèn)false,不稀疏】
是否定時(shí)刪除(過(guò)期索引):
db.article.ensureIndex({ "time" : 1 } , { "expireAfterSeconds" : 30})
查看索引
db.collection.getIndexes()