共计 5425 个字符,预计需要花费 14 分钟才能阅读完成。
当初咱们就开始实操 mongodb 的索引吧
数据筹备
向 mydoc 汇合中,插入多条数据,mydoc 之前是没有存在过的,咱们间接应用 db.mydoc.insertMany()
,mongodb 会默认给咱们新建这个汇合
db.mydoc.insertMany([{ item:"canvas", qty:120, size:{ h:28, w:35.5, uom:"cm"}, status:"A", createDate:ISODate("2016-02-06T20:20:13Z") },
{item:"journal", qty:25, tags:[ {tag:"gray", type:"paper"}, {tag:"red", type:"electron"} ], size:{h:14, w:21, uom:"cm"}, status:"A", createDate:ISODate("2016-02-07T20:20:13Z") },
{item:"notebook", qty:50, tags:[ {tag:"yellow", type:"paper"}, {tag:"green", type:"electron"}], size:{h:8.5, w:11, uom:"in"}, status:"P", createDate:ISODate("2016-02-08T20:20:13Z")},
{item:"paper", qty:100, tags:[{tag:"yellow", type:"paper"}, {tag:"brown", type:"electron"}], size:{h:8.5, w:11, uom:"in"}, status:"D", createDate:ISODate("2016-02-09T20:20:13Z") },
{item:"planner", qty:75, tags:[{tag:"yellow", type:"paper"}, {tag:"green", type:"electron"}], size:{h:22.85, w:30, uom:"cm"}, status:"D", createDate:ISODate("2016-02-10T20:20:13Z") },
{item:"postcard", qty:45, tags:[{tag:"black", type:"paper"}, {tag:"green", type:"electron"}], size:{h:10, w:15.25, uom:"cm"}, status:"P", createDate:ISODate("2016-02-11T20:20:13Z") },
{item:"sketchbook", qty:80, status:"A", createDate:ISODate("2016-02-12T20:20:13Z") }
]);
插入胜利
单字段索引
应用单字段索引,依据物品名称查问物品
db.mydoc.createIndex({item:1})
应用 db.mydoc.getIndexes()
查看所有索引,能够查看到方才咱们创立的索引 item_1,其中 _id_
是默认索引
> db.mydoc.getIndexes()
[
{
"v" : 2,
"key" : {"_id" : 1},
"name" : "_id_",
"ns" : "mytest.mydoc"
},
{
"v" : 2,
"key" : {"item" : 1},
"name" : "item_1",
"ns" : "mytest.mydoc"
}
]
>
咱们来查问一下数据,看看是否命中索引
> db.mydoc.find().sort({item:1}).explain()
通过上图咱们能够看到,曾经命中索引,并且索引范畴是 [MinKey, MaxKey],如果咱们查问的时候,sort 外面排序为倒序(-1),那么此处的索引范畴就是到过去的 [MaxKey, MinKey],感兴趣的 xdm 能够尝试一下
尝试不加 sort
如果咱们间接 db.mydoc.find().explain()
是不会命中索引的,,mongodb 会默认走 全文索引
复合索引
索引的程序跟查问排序相关联
创立复合索引,status 字段 做升序,qty 字段做降序
db.mydoc.createIndex({status:1, qty:-1})
咱们创立的索引一升一降,查问排序的模式必须与索引键的模式匹配或逆向,也就是说,咱们查问的时候,
能够是 {status:-1, qty:1}
也能够是{status:1, qty:-1}
,
然而不能 {status:-1, qty:-1}
也不能 {status:1, qty:1}
因为这样的查问程序是和咱们的索引矛盾的,这两种模式是不能被命中索引的
TLL 索引
数据筹备
新建一个 日志汇合,插入多条数据,带上最初批改的工夫
db.eventlog.insert(
[{system:"trade", lastModifiedDate:ISODate("2017-11-12T20:20:13Z"), context:"NullPointException,"},
{system:"goods", lastModifiedDate:ISODate("2017-11-15T20:21:13Z"), context:"NullPointException,"},
{system:"mongodb", lastModifiedDate:ISODate("2017-11-16T20:22:13Z"), context:"2019-11-12 18:18:52.426 [main] DEBUG org.mongodb.driver.connection - Closing connection connectionId{localValue:2, serverValue:2409}"}
]
)
执行后果
查问一下 eventlog
> db.eventlog.find()
{"_id" : ObjectId("615eb334631f5c41fb6c6c16"), "system" : "trade", "lastModifiedDate" : ISODate("2017-11-12T20:20:13Z"), "context" : "NullPointException," }
{"_id" : ObjectId("615eb334631f5c41fb6c6c17"), "system" : "goods", "lastModifiedDate" : ISODate("2017-11-15T20:21:13Z"), "context" : "NullPointException," }
{"_id" : ObjectId("615eb334631f5c41fb6c6c18"), "system" : "mongodb", "lastModifiedDate" : ISODate("2017-11-16T20:22:13Z"), "context" : "2019-11-12 18:18:52.426 [main] DEBUG org.mongodb.driver.connection - Closing connection connectionId{localValue:2, serverValue:2409}" }
创立一个 TLL 索引
创立索引的字段是日期或者是日期数组,不是这种类型的字段,是不会删除文档的
设置 30 秒 后过期,会话、日志,会话过期后会删除汇合
> db.eventlog.createIndex({"lastModifiedDate":1}, {expireAfterSeconds:30})
30 s 之后,咱们再来查问一下数据
db.eventlog.find()
果然是查问不到后果的,文档数据被删除掉了,索引还会在吗?
> db.eventlog.getIndexes()
hash 索引
数据筹备
插入一些数据
db.mydoc.drop() // 清空表
db.mydoc.insertMany([{ item:"canvas", qty:120, size:{ h:28, w:35.5, uom:"cm"}, status:"A", createDate:ISODate("2016-02-06T20:20:13Z") },
{item:"journal", qty:25, tags:[ {tag:"gray", type:"paper"}, {tag:"red", type:"electron"} ], size:{h:14, w:21, uom:"cm"}, status:"A", createDate:ISODate("2016-02-07T20:20:13Z") },
{item:"notebook", qty:50, tags:[ {tag:"yellow", type:"paper"}, {tag:"green", type:"electron"}], size:{h:8.5, w:11, uom:"in"}, status:"P", createDate:ISODate("2016-02-08T20:20:13Z")},
{item:"paper", qty:100, tags:[{tag:"yellow", type:"paper"}, {tag:"brown", type:"electron"}], size:{h:8.5, w:11, uom:"in"}, status:"D", createDate:ISODate("2016-02-09T20:20:13Z") },
{item:"planner", qty:75, tags:[{tag:"yellow", type:"paper"}, {tag:"green", type:"electron"}], size:{h:22.85, w:30, uom:"cm"}, status:"D", createDate:ISODate("2016-02-10T20:20:13Z") },
{item:"postcard", qty:45, tags:[{tag:"black", type:"paper"}, {tag:"green", type:"electron"}], size:{h:10, w:15.25, uom:"cm"}, status:"P", createDate:ISODate("2016-02-11T20:20:13Z") },
{item:"sketchbook", qty:80, status:"A", createDate:ISODate("2016-02-12T20:20:13Z") }
]);
创立 hash 索引
> db.mydoc.createIndex({item:"hashed"})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
查看 hash 索引是否命中
> db.mydoc.find({item:"paper"}).explain()
图中能够看出,IXSCAN 示意为曾经命中 hash 索引
空间索引
有 二维索引 和 球体索引,官网上能够看这里
https://docs.mongodb.com/manu…
咱们来实际一下 球体索引
球体空间索引,2dsphere。
反对相似地球球体上的地位,能够寄存 GeoJSON、传统坐标类型的数据。
GeoJSON 数据
须要应用嵌入式文档寄存,coordinates 指定坐标地位,type 指定坐标类型
Type 有如下 3 种模式
- point
例如能够这样写:location: {type: "Point", coordinates: [-33.856077, 30.848447] }
- lineString
例如能够这样写:location: {type: "LineString", coordinates: [ [ 40, 5], [41, 6] ] }
- polygon
例如能够这样写:`location: {type: “Polygon”,
coordinates: [[ [ 0 , 0] , [3 , 6] , [6 , 1] , [0 , 0] ] ]
}`
传统坐标数据
一个字段即可指定坐标地位。
GeoJSON 数据 和 传统坐标数据 两种类型数据,经纬度的存储形式必须是 [经度, 纬度] 的数组模式
开始实际,数据筹备
在 places 汇合中插入 2 个文档数据
db.places.insert([
{loc:{ type:"Point", coordinates:[ -73.97, 40.77] },
name:"Central Park", category:"Parks"
},
{loc:{ type:"Point", coordinates:[ -73.88, 40.78] },
name:"La Guardia Airport", category:"Airport"
}
]);
创立球体空间索引
db.places.createIndex({ loc:"2dsphere"} )
查看索引
> db.places.getIndexes()
创立空间索引的复合索引
以 category 降序,name 升序
db.places.createIndex({ loc:"2dsphere" , category:-1, name:1} )
查看索引能够看到
欢送点赞,关注,珍藏
敌人们,你的反对和激励,是我保持分享,提高质量的能源
好了,本次就到这里
技术是凋谢的,咱们的心态,更应是凋谢的。拥抱变动,背阴而生,致力向前行。
我是 阿兵云原生,欢送点赞关注珍藏,下次见~