MongoDB索引類型匯總分享
MongoDB 4.2官方支持索引類型如下:
- 單字段索引
- 復(fù)合索引
- 多鍵索引
- 文本索引
- 2dsphere索引
- 2d索引
- geoHaystack索引
- 哈希索引
單字段索引
在單個(gè)字段上創(chuàng)建升序索引
handong1:PRIMARY> db.test.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "db6.test"
}
]
在字段id上添加升序索引
handong1:PRIMARY> db.test.createIndex({"id":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1621322378, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1621322378, 1)
}
handong1:PRIMARY> db.test.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "db6.test"
},
{
"v" : 2,
"key" : {
"id" : 1
},
"name" : "id_1",
"ns" : "db6.test"
}
]
handong1:PRIMARY> db.test.find({"id":100})
{ "_id" : ObjectId("60a35d061f183b1d8f092114"), "id" : 100, "name" : "handong", "ziliao" : { "name" : "handong", "age" : 25, "hobby" : "mongodb" } }
上述查詢可以使用新建的單字段索引。
在嵌入式字段上創(chuàng)建索引
handong1:PRIMARY> db.test.createIndex({"ziliao.name":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1621323677, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1621323677, 2)
}
以下查詢可以用的新建的索引。
db.test.find({"ziliao.name":"handong"})
在內(nèi)嵌文檔上創(chuàng)建索引
handong1:PRIMARY> db.test.createIndex({ziliao:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 3,
"numIndexesAfter" : 4,
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1621324059, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1621324059, 2)
}
以下查詢可以使用新建的索引。
db.test.find({ziliao:{ "name" : "handong", "age" : 25, "hobby" : "mongodb" }})
復(fù)合索引
創(chuàng)建復(fù)合索引
db.user.createIndex({"product_id":1,"type":-1})
以下查詢可以用到新建的復(fù)合索引
db.user.find({"product_id":"e5a35cfc70364d2092b8f5d14b1a3217","type":0})
多鍵索引
基于一個(gè)數(shù)組創(chuàng)建索引,MongoDB會(huì)自動(dòng)創(chuàng)建為多鍵索引,無需刻意指定。
多鍵索引也可以基于內(nèi)嵌文檔來創(chuàng)建。
多鍵索引的邊界值的計(jì)算依賴于特定的規(guī)則。
查看文檔:
handong1:PRIMARY> db.score.find()
{ "_id" : ObjectId("60a32d7f1f183b1d8f0920ad"), "name" : "dandan", "age" : 30, "score" : [ { "english" : 90, "math" : 99, "physics" : 88 } ], "is_del" : false }
{ "_id" : ObjectId("60a32d8b1f183b1d8f0920ae"), "name" : "dandan", "age" : 30, "score" : [ 99, 98, 97, 96 ], "is_del" : false }
{ "_id" : ObjectId("60a32d9a1f183b1d8f0920af"), "name" : "dandan", "age" : 30, "score" : [ 100, 100, 100, 100 ], "is_del" : false }
{ "_id" : ObjectId("60a32e8c1f183b1d8f0920b0"), "name" : "dandan", "age" : 30, "score" : [ { "english" : 70, "math" : 99, "physics" : 88 } ], "is_del" : false }
{ "_id" : ObjectId("60a37b141f183b1d8f0aa751"), "name" : "dandan", "age" : 30, "score" : [ 96, 95 ] }
{ "_id" : ObjectId("60a37b1d1f183b1d8f0aa752"), "name" : "dandan", "age" : 30, "score" : [ 96, 95, 94 ] }
{ "_id" : ObjectId("60a37b221f183b1d8f0aa753"), "name" : "dandan", "age" : 30, "score" : [ 96, 95, 94, 93 ] }
創(chuàng)建score字段多鍵索引:
db.score.createIndex("score":1)
handong1:PRIMARY> db.score.find({"score":[ 96, 95 ]})
{ "_id" : ObjectId("60a37b141f183b1d8f0aa751"), "name" : "dandan", "age" : 30, "score" : [ 96, 95 ] }
查看執(zhí)行計(jì)劃:
handong1:PRIMARY> db.score.find({"score":[ 96, 95 ]}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "db6.score",
"indexFilterSet" : false,
"parsedQuery" : {
"score" : {
"$eq" : [
96,
95
]
}
},
"queryHash" : "8D76FC59",
"planCacheKey" : "E2B03CA1",
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"score" : {
"$eq" : [
96,
95
]
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"score" : 1
},
"indexName" : "score_1",
"isMultiKey" : true,
"multiKeyPaths" : {
"score" : [
"score"
]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"score" : [
"[96.0, 96.0]",
"[[ 96.0, 95.0 ], [ 96.0, 95.0 ]]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "mongo3",
"port" : 27017,
"version" : "4.2.12",
"gitVersion" : "5593fd8e33b60c75802edab304e23998fa0ce8a5"
},
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1621326912, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1621326912, 1)
}
可以看到已經(jīng)使用了新建的多鍵索引。
文本索引
為了支持對(duì)字符串內(nèi)容的文本搜索查詢,MongoDB提供了文本索引。文本(text )索引可以包含任何值為字符串或字符串元素?cái)?shù)組的字段
db.user.createIndex({"sku_attributes":"text"})
db.user.find({$text:{$search:"測(cè)試"}})
查看執(zhí)行計(jì)劃:
handong1:PRIMARY> db.user.find({$text:{$search:"測(cè)試"}}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "db6.user",
"indexFilterSet" : false,
"parsedQuery" : {
"$text" : {
"$search" : "測(cè)試",
"$language" : "english",
"$caseSensitive" : false,
"$diacriticSensitive" : false
}
},
"queryHash" : "83098EE1",
"planCacheKey" : "7E2D582B",
"winningPlan" : {
"stage" : "TEXT",
"indexPrefix" : {
},
"indexName" : "sku_attributes_text",
"parsedTextQuery" : {
"terms" : [
"測(cè)試"
],
"negatedTerms" : [ ],
"phrases" : [ ],
"negatedPhrases" : [ ]
},
"textIndexVersion" : 3,
"inputStage" : {
"stage" : "TEXT_MATCH",
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "OR",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"_fts" : "text",
"_ftsx" : 1
},
"indexName" : "sku_attributes_text",
"isMultiKey" : true,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "backward",
"indexBounds" : {
}
}
}
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "mongo3",
"port" : 27017,
"version" : "4.2.12",
"gitVersion" : "5593fd8e33b60c75802edab304e23998fa0ce8a5"
},
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1621328543, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1621328543, 1)
}
可以看到通過文本索引可以查到包含測(cè)試關(guān)鍵字的數(shù)據(jù)。
**注意:**可以根據(jù)自己需要?jiǎng)?chuàng)建復(fù)合文本索引。
2dsphere索引
創(chuàng)建測(cè)試數(shù)據(jù)
db.places.insert(
{
loc : { type: "Point", coordinates: [ 116.291226, 39.981198 ] },
name: "火器營(yíng)橋",
category : "火器營(yíng)橋"
}
)
db.places.insert(
{
loc : { type: "Point", coordinates: [ 116.281452, 39.914226 ] },
name: "五棵松",
category : "五棵松"
}
)
db.places.insert(
{
loc : { type: "Point", coordinates: [ 116.378038, 39.851467 ] },
name: "角門西",
category : "角門西"
}
)
db.places.insert(
{
loc : { type: "Point", coordinates: [ 116.467833, 39.881581 ] },
name: "潘家園",
category : "潘家園"
}
)
db.places.insert(
{
loc : { type: "Point", coordinates: [ 116.468264, 39.914766 ] },
name: "國(guó)貿(mào)",
category : "國(guó)貿(mào)"
}
)
db.places.insert(
{
loc : { type: "Point", coordinates: [ 116.46618, 39.960213 ] },
name: "三元橋",
category : "三元橋"
}
)
db.places.insert(
{
loc : { type: "Point", coordinates: [ 116.400064, 40.007827 ] },
name: "奧林匹克森林公園",
category : "奧林匹克森林公園"
}
)
添加2dsphere索引
db.places.createIndex( { loc : "2dsphere" } )
db.places.createIndex( { loc : "2dsphere" , category : -1, name: 1 } )
利用2dsphere索引查詢多邊形里的點(diǎn)
鳳凰嶺
[116.098234,40.110569]
天安門
[116.405239,39.913839]
四惠橋
[116.494351,39.912068]
望京
[116.494494,40.004594]
handong1:PRIMARY> db.places.find( { loc :
... { $geoWithin :
... { $geometry :
... { type : "Polygon" ,
... coordinates : [ [
... [116.098234,40.110569] ,
... [116.405239,39.913839] ,
... [116.494351,39.912068] ,
... [116.494494,40.004594] ,
... [116.098234,40.110569]
... ] ]
... } } } } )
{ "_id" : ObjectId("60a4c950d4211a77d22bf7f8"), "loc" : { "type" : "Point", "coordinates" : [ 116.400064, 40.007827 ] }, "name" : "奧林匹克森林公園", "category" : "奧林匹克森林公園" }
{ "_id" : ObjectId("60a4c94fd4211a77d22bf7f7"), "loc" : { "type" : "Point", "coordinates" : [ 116.46618, 39.960213 ] }, "name" : "三元橋", "category" : "三元橋" }
{ "_id" : ObjectId("60a4c94fd4211a77d22bf7f6"), "loc" : { "type" : "Point", "coordinates" : [ 116.468264, 39.914766 ] }, "name" : "國(guó)貿(mào)", "category" : "國(guó)貿(mào)" }
可以看到把集合中包含在指定四邊形里的點(diǎn),全部列了出來。
利用2dsphere索引查詢球體上定義的圓內(nèi)的點(diǎn)
handong1:PRIMARY> db.places.find( { loc :
... { $geoWithin :
... { $centerSphere :
... [ [ 116.439518, 39.954751 ] , 2/3963.2 ]
... } } } )
{ "_id" : ObjectId("60a4c94fd4211a77d22bf7f7"), "loc" : { "type" : "Point", "coordinates" : [ 116.46618, 39.960213 ] }, "name" : "三元橋", "category" : "三元橋" }
返回所有半徑為經(jīng)度 116.439518 E 和緯度 39.954751 N 的2英里內(nèi)坐標(biāo)。示例將2英里的距離轉(zhuǎn)換為弧度,通過除以地球近似的赤道半徑3963.2英里。
2d索引
在以下情況下使用2d索引:
- 您的數(shù)據(jù)庫具有來自MongoDB 2.2或更早版本的舊版舊版坐標(biāo)對(duì)。
- 您不打算將任何位置數(shù)據(jù)存儲(chǔ)為GeoJSON對(duì)象。
哈希索引
要?jiǎng)?chuàng)建hashed索引,請(qǐng)指定 hashed 作為索引鍵的值,如下例所示:
handong1:PRIMARY> db.test.createIndex({"_id":"hashed"})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 4,
"numIndexesAfter" : 5,
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1621419338, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1621419338, 1)
}
注意事項(xiàng)
- MongoDB支持任何單個(gè)字段的 hashed 索引。hashing函數(shù)折疊嵌入的文檔并計(jì)算整個(gè)值的hash值,但不支持多鍵(即.數(shù)組)索引。
- 您不能創(chuàng)建具有hashed索引字段的復(fù)合索引,也不能在索引上指定唯一約束hashed;但是,您可以hashed在同一字段上創(chuàng)建索引和升序/降序(即非哈希)索引:MongoDB將對(duì)范圍查詢使用標(biāo)量索引。
到此這篇關(guān)于MongoDB索引類型匯總分享的文章就介紹到這了,更多相關(guān)MongoDB索引內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MongoDB使用小結(jié):一些不常見的經(jīng)驗(yàn)分享
最近一年忙碌于數(shù)據(jù)處理相關(guān)的工作,跟MongoDB打交道極多,以下為實(shí)踐過程中的Q&A,后續(xù)會(huì)不定期更新補(bǔ)充2017-03-03
Linux系統(tǒng)下安裝MongoDB的詳細(xì)方法圖文教程
這篇文章主要介紹了Linux系統(tǒng)下安裝MongoDB的詳細(xì)方法圖文教程,需要的朋友可以參考下2023-06-06
ubuntu mongodb安裝在哪個(gè)文件夾路徑詳解
這篇文章主要為大家介紹了ubuntu mongodb安裝在哪個(gè)文件夾的安裝路徑詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
MongoDB數(shù)據(jù)庫查詢性能提高40倍的經(jīng)歷分享
大家在使用 MongoDB 的時(shí)候有沒有碰到過性能問題呢?下面這篇文章主要給大家分享了MongoDB數(shù)據(jù)庫查詢性能提高40倍的經(jīng)歷,需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02
Mongodb基本操作與Python連接mongodb并進(jìn)行基礎(chǔ)操作的方法
mongodb是基于分布式文件存儲(chǔ)的nosql(非關(guān)系型)數(shù)據(jù)庫,本文分享了mongodb的基礎(chǔ)操作和Python連接并操作mongodb的基礎(chǔ)方法,基礎(chǔ)的不能再基礎(chǔ)了2018-09-09
MongoDB安全及身份認(rèn)證(實(shí)例講解)
下面小編就為大家?guī)硪黄狹ongoDB安全及身份認(rèn)證(實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
MongoDB通過查詢與游標(biāo)徹底玩轉(zhuǎn)分布式文件存儲(chǔ)
MongoDB最大的特點(diǎn)是它支持的查詢語言非常強(qiáng)大,其語法有點(diǎn)類似于面向?qū)ο蟮牟樵冋Z言,幾乎可以實(shí)現(xiàn)類似關(guān)系數(shù)據(jù)庫單表查詢的絕大部分功能,而且還支持對(duì)數(shù)據(jù)建立索引,這篇文章主要介紹了MongoDB查詢與游標(biāo),徹底玩轉(zhuǎn)分布式文件存儲(chǔ),需要的朋友可以參考下2023-01-01

