本篇文章带你了解MongoDB,介绍一下MongoDB中丰富的索引类型,希望对大家有所帮助!
|
本篇文章带你了解MongoDB,介绍一下MongoDB中丰富的索引类型,希望对大家有所帮助!
在 为了下面方便测试我们使用脚本插入以下数据 for(var i = 0;i < 100000;i++){
db.users.insertOne({
username: "user"+i,
age: Math.random() * 100,
sex: i % 2,
phone: 18468150001+i
});
}单键索引单键索引即索引的字段只有一个,是最基础的索引方式. 在集合中使用 db.users.createIndex({username:1})
'username_1'在创建索引后查看一下使用 db.users.find({username:"user40001"}).explain()
{
queryPlanner:
{
winningPlan:
{
......
stage: 'FETCH',
inputStage:
{
stage: 'IXSCAN',
keyPattern: { username: 1 },
indexName: 'username_1',
......
}
}
rejectedPlans: [] ,
},
......
ok: 1
} 在索引优化的原则当中,有很重要的原则就是索引要建立在基数高的的字段上,所谓基数就是一个字段上不重复数值的个数,即我们在创建 下面就船舰一个 db.users.createIndex({sex:1})
'sex_1'
db.users.find({sex:1}).explain()
{
queryPlanner:
{
......
winningPlan:
{
stage: 'COLLSCAN',
filter: { sex: { '$eq': 1 } },
direction: 'forward'
},
rejectedPlans: []
},
......
ok: 1
}联合索引联合索引即索引上会有多个字段,下面使用 db.users.createIndex({age:1,sex:1})
'age_1_sex_1'然后我们使用这两个字段进行一次查询,查看执行计划,顺利地走了这条索引 db.users.find({age:23,sex:1}).explain()
{
queryPlanner:
{
......
winningPlan:
{
stage: 'FETCH',
inputStage:
{
stage: 'IXSCAN',
keyPattern: { age: 1, sex: 1 },
indexName: 'age_1_sex_1',
.......
indexBounds: { age: [ '[23, 23]' ], sex: [ '[1, 1]' ] }
}
},
rejectedPlans: [],
},
......
ok: 1
}数组索引数组索引就是对数组字段创建索引,也叫做多值索引,下面为了测试将 db.users.updateOne({username:"user1"},{$set:{hobby:["唱歌","篮球","rap"]}})
......创建数组索引并进行查看其执行计划,注意 db.users.createIndex({hobby:1})
'hobby_1'
db.users.find({hobby:{$elemMatch:{$eq:"钓鱼"}}}).explain()
{
queryPlanner:
{
......
winningPlan:
{
stage: 'FETCH',
filter: { hobby: { '$elemMatch': { '$eq': '钓鱼' } } },
inputStage:
{
stage: 'IXSCAN',
keyPattern: { hobby: 1 },
indexName: 'hobby_1',
isMultiKey: true,
multiKeyPaths: { hobby: [ 'hobby' ] },
......
indexBounds: { hobby: [ '["钓鱼", "钓鱼"]' ] } }
},
rejectedPlans: []
},
......
ok: 1
} 数组索引相比于其它索引来说索引条目和体积必然呈倍数增加,例如平均每个文档的 联合数组索引 联合数组索引就是含有数组字段的联合索引,这种索引不支持一个索引中含有多个数组字段,即一个索引中最多能有一个数组字段,这是为了避免索引条目爆炸式增长,假设一个索引中有两个数组字段,那么这个索引条目的数量将是普通索引的n*m倍 地理空间索引在原先的 for(var i = 0;i < 100000;i++){
db.users.updateOne(
{username:"user"+i},
{
$set:{
location:{
type: "Point",
coordinates: [100+Math.random() * 4,40+Math.random() * 3]
}
}
});
}创建一个二维空间索引 db.users.createIndex({location:"2dsphere"})
'location_2dsphere'
//查询500米内的人
db.users.find({
location:{
$near:{
$geometry:{type:"Point",coordinates:[102,41.5]},
$maxDistance:500
}
}
})地理空间索引的 TTL索引 TTL的全拼是 当前时间?TTL索引字段时间>expireAfterSrconds
首先在我们文档上增减一个时间字段 for(var i = 90000;i < 100000;i++){
db.users.updateOne(
{username:"user"+i},
{
$set:{
createdDate:new Date()
}
});
}创建一个TTL索引并且设定过期时间为60s,待过60s后查询,会发现这些数据已经不存在 db.users.createIndex({createdDate:1},{expireAfterSeconds:60})
'createdDate_1'另外还可以用 db.runCommand({
collMod:"users",
index:{
keyPattern:{createdDate:1},
expireAfterSeconds:120
}
})
{ expireAfterSeconds_old: 60, expireAfterSeconds_new: 120, ok: 1 }条件索引条件索引也叫部分索引(partial),只对满足条件的数据进行建立索引. 只对50岁以上的 db.users.createIndex({username:1},{partialFilterExpression:{
age:{$gt:50}
}})
'username_1'
db.users.find({$and:[{username:"user4"},{age:60}]}).explain()
{
queryPlanner:
{
......
winningPlan:
{
stage: 'FETCH',
filter: { age: { '$eq': 60 } },
inputStage:
{
stage: 'IXSCAN',
keyPattern: { username: 1 },
indexName: 'username_1',
......
isPartial: true,
......
}
},
rejectedPlans: []
},
......
ok: 1
}稀疏索引 一般的索引会根据某个字段为整个集合创建一个索引,即使某个文档不存这个字段,那么这个索引会把这个文档的这个字段当作 稀疏索引不会对文档中不存在的字段建立索引,如果这个字段存在但是为 下面给 for(var i = 5000;i < 10000;i++){
if(i < 9000){
db.users.updateOne(
{username:"user"+i},
{ $set:{email:(120000000+i)+"@qq.email"}}
)
}else{
db.users.updateOne(
{username:"user"+i},
{ $set:{email:null}}
)
}
}当不建立索引使用 db.users.find({email:null})
{
_id: ObjectId("61bdc01ba59136670f6536fd"),
username: 'user0',
age: 64.41483801726282,
sex: 0,
phone: 18468150001,
location:
{
type: 'Point',
coordinates: [ 101.42490900320335, 42.2576650823515 ]
}
}
...... 然后对 db.users.createIndex({email:1},{sparse:true});
'email_1'
db.users.find({email:null}).hint({email:1})
{
_id: ObjectId("61bdc12ca59136670f655a25"),
username: 'user9000',
age: 94.18397576757012,
sex: 0,
phone: 18468159001,
hobby: [ '钓鱼', '乒乓球' ],
location:
{
type: 'Point',
coordinates: [ 101.25903151863596, 41.38450145025062 ]
},
email: null
}
......文本索引文本索引将建立索引的文档字段先进行分词再进行检索,但是目前还不支持中文分词. 下面增加两个文本字段,创建一个联合文本索引 db.blog.insertMany([
{title:"hello world",content:"mongodb is the best database"},
{title:"index",content:"efficient data structure"}
])
//创建索引
db.blog.createIndex({title:"text",content:"text"})
'title_text_content_text'
//使用文本索引查询
db.blog.find({$text:{$search:"hello data"}})
{
_id: ObjectId("61c092268c4037d17827d977"),
title: 'index',
content: 'efficient data structure'
},
{
_id: ObjectId("61c092268c4037d17827d976"),
title: 'hello world',
content: 'mongodb is the best database'
}唯一索引唯一索引就是在建立索引地字段上不能出现重复元素,除了单字段唯一索引还有联合唯一索引以及数组唯一索引(即数组之间不能有元素交集 ) //对title字段创建唯一索引
db.blog.createIndex({title:1},{unique:true})
'title_1'
//插入一个已经存在的title值
db.blog.insertOne({title:"hello world",content:"mongodb is the best database"})
MongoServerError: E11000 duplicate key error collection: mock.blog index: title_1 dup key: { : "hello world" }
//查看一下执行计划,isUnique为true
db.blog.find({"title":"index"}).explain()
{
queryPlanner:
{
......
winningPlan:
{
stage: 'FETCH',
inputStage:
{
stage: 'IXSCAN',
keyPattern: { title: 1 },
indexName: 'title_1',
isMultiKey: false,
multiKeyPaths: { title: [] },
isUnique: true,
......
}
},
rejectedPlans: []
},
.......
ok: 1
}相关视频教程推荐:《MongoDB教程》 以上就是带你聊聊MongoDB中丰富的索引类型的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章! |
