mongodb之索引学习
来源:互联网 发布:小米手机怎么迁移数据 编辑:程序博客网 时间:2024/05/21 06:25
学习索引分类和创建索引:
1._id索引 大多数集合默认的索引
2.单键索引:手动创建,一个单一的值
3.多建索引:组合函数
4.复合索引 :最左前缀原则
5.过期索引 :一定时间内失效,注意点:必须是isodate或者其数组,不要使用时间戳,否则不会被自动删除。
6.全文索引 db.tm.ensureindex({"article":"text"}),db.tm.ensureindex({"key1":"text","key2":"text"}),db.tm.ensureindex({$**:"text"})
查询:db.tm.find({$text:{$search:“aa”}})
db.tm.find({$text:{$search:"aa bb cc "}})
db.tm.find({$text:{$search:"aa bb -cc"}})
db.tm.find({$text:{$search:"\"a\"\"bb"\"cc\""}})
全文索引的匹配度$meta
db.tm.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}})
db.tm.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}})
每次只能指定$text
7.地理位置索引(滴滴打车,大众点评)
2d索引(地址位置索引)
db.localtion.ensureindex({"w":"2d"})
查看索引:db.tm.getIndexes()
建索引db.t1.ensureIndex({x:1})
多键索引db.tm.ensureIndex({x:[1,2,3,4,5]})
复合索引 db.tm.ensureIndex({x:1,y:1})
删除索引db.tm.dropIndex("x_1_y_1")
db.tm.find({x:100,y:100}).explain()
过期索引:db.tm.insert({time:new Date()}) ISOdate 就是当前时间
db.tm.ensureIndex({time:1},{expireAfterSeconds:10})
db.tm.insert({time:new Date(),z:1})
全文索引
db.t1.ensureIndex({article:"text"})
db.t1.insert({article:"aa bb cc"})
查找db.t1.find({$text:{$search:"aa"}})
db.t1.find({$text:{$search:"aa bb cc"}})或关系
db.t1.find({$text:{$search:"aa bb -cc"}})不包含CC
db.t1.find({$text:{$search:"\"aa\"\" bb\"\" -cc\""}})且的关系
全文索引的相似度:db.t1.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}})
db.t1.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}})
索引命名
db.t1.ensureIndex({x:1,y:2,z:2},{name:"xyz"})
db.t1.dropIndex("xyz")
创建唯一索引
db.t2.ensureIndex({m:1,n:1},{unique:true})
查看s索引存在某个字段
db.abc.find({m:{$exists:true}})
创建2d索引:平面地理位置索引,位置表示方式,经纬度[经度(-180,180),纬度(-90,90)]
db.location.ensureIndex({"w":"2d"})
db.location.insert({w:[1,1]})
db.location.insert({w:[1,2]})
db.location.insert({w:[3,2]})
db.location.insert({w:[32,22]})
db.location.insert({w:[100,90]})
就近查询
db.location.find({w:{$near:[1,1]}})
查询
db.location.find({w:{$near:[1,1],$maxDistance:10}})
地址位置索引
geoNear
db.runCommand({geoNear:"location",near:[1,2],maxDistance:10,num:1})
db.stats
for(i=1;i<10000;i++)db.tt.insert({n:i})
查看运行状态
/export/mongodb/bin/mongostat -h 192.168.1.70:22222
faults locked idx miss没有使用索引值
qr|qw读写队列
查看当前级别
db.getProfilingStatus()
0:profile为关闭,mongodb不会记录任何操作
1配合slowms使用,mongodb会记录任何超过slowms的操作
2会记录任何记录
修改级别profile
db.setProfilingLevel(0)
db.system.profile.find().sort({$natural:1}).limit(10)
查询排序
db.system.indexes.find().sort({$nature:1})
- mongodb之索引学习
- MongoDB学习整理之索引
- mongodb学习记录之三:索引
- MongoDB学习之旅十八:索引
- mongodb学习:mongodb索引原理
- MongoDB 索引之全文索引
- MongoDB学习笔记(索引)
- MongoDB学习笔记(索引)
- MongoDB索引学习笔记
- MongoDB学习笔记(索引)
- MongoDB学习笔记(索引)
- mongodb学习(索引详解)
- MongoDB学习笔记(索引)
- MongoDB学习 索引
- MongoDB学习笔记(索引)
- MongoDB学习笔记(索引)
- Mongodb学习-索引
- MongoDB学习笔记(索引)
- 转载的动态表白程序
- Spring MVC基础
- 《数字技术》连载16: 第2章 数字器件 第11节 JK 触发器
- linux 中如何将两个iso合成一个
- dp专题:编辑距离
- mongodb之索引学习
- Linux下JDK1.7升级1.8版本
- UE4简单的对物体进行可破坏效果的制作
- T-SQL编程上课例题
- JS:根据出生日期 得到年龄
- 面试题总结——JAVA高级工程师(三)
- MongoDB基础操作
- Oracle Cursor
- <5> . Effective C++阅读