<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
MongoDB 4.2官方支援索引型別如下:
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" } }
上述查詢可以使用新建的單欄位索引。
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"})
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" }})
建立複合索引
db.user.createIndex({"product_id":1,"type":-1})
以下查詢可以用到新建的複合索引
db.user.find({"product_id":"e5a35cfc70364d2092b8f5d14b1a3217","type":0})
基於一個陣列建立索引,MongoDB會自動建立為多鍵索引,無需刻意指定。
多鍵索引也可以基於內嵌檔案來建立。
多鍵索引的邊界值的計算依賴於特定的規則。
檢視檔案:
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 ] }
建立score欄位多鍵索引:
db.score.createIndex("score":1)
handong1:PRIMARY> db.score.find({"score":[ 96, 95 ]}) { "_id" : ObjectId("60a37b141f183b1d8f0aa751"), "name" : "dandan", "age" : 30, "score" : [ 96, 95 ] }
檢視執行計劃:
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) }
可以看到已經使用了新建的多鍵索引。
為了支援對字串內容的文字搜尋查詢,MongoDB提供了文字索引。文字(text )索引可以包含任何值為字串或字串元素陣列的欄位
db.user.createIndex({"sku_attributes":"text"})
db.user.find({$text:{$search:"測試"}})
檢視執行計劃:
handong1:PRIMARY> db.user.find({$text:{$search:"測試"}}).explain() { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "db6.user", "indexFilterSet" : false, "parsedQuery" : { "$text" : { "$search" : "測試", "$language" : "english", "$caseSensitive" : false, "$diacriticSensitive" : false } }, "queryHash" : "83098EE1", "planCacheKey" : "7E2D582B", "winningPlan" : { "stage" : "TEXT", "indexPrefix" : { }, "indexName" : "sku_attributes_text", "parsedTextQuery" : { "terms" : [ "測試" ], "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) }
可以看到通過文字索引可以查到包含測試關鍵字的資料。
**注意:**可以根據自己需要建立複合文字索引。
db.places.insert( { loc : { type: "Point", coordinates: [ 116.291226, 39.981198 ] }, name: "火器營橋", category : "火器營橋" } ) 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: "國貿", category : "國貿" } ) 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 : "奧林匹克森林公園" } )
db.places.createIndex( { loc : "2dsphere" } )
db.places.createIndex( { loc : "2dsphere" , category : -1, name: 1 } )
鳳凰嶺
[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" : "國貿", "category" : "國貿" }
可以看到把集合中包含在指定四邊形裡的點,全部列了出來。
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" : "三元橋" }
返回所有半徑為經度 116.439518 E 和緯度 39.954751 N 的2英里內座標。範例將2英里的距離轉換為弧度,通過除以地球近似的赤道半徑3963.2英里。
在以下情況下使用2d索引:
要建立hashed索引,請指定 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) }
注意事項
到此這篇關於MongoDB索引型別彙總分享的文章就介紹到這了,更多相關MongoDB索引內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45