索引使用场景:优秀:加快查询速度不足:增删改查会产生额外的开销和占用空间提示:返回集合中一半以上的数据,全表扫描效率高。创建索引:db.test.ensureIndex({"username":1},{"background":true,"name":"index_test_name"})//当数据量较大时,可以在后台不阻塞删除索引:db。test.dropIndex({"username":1})检查索引大小:db.test.totalIndexSize()属性索引顺序:1为正序,-1为倒序。注意复合索引中的顺序(id:1,age:-1)索引属性:唯一性db.test.ensureIndex({x:1,y:1},{unique:true})稀疏性db.test。ensureIndexx({},{sparse:true/false})noSparse(default):1.索引字段不存在的数据可以插入,null;2、不存在的字段可以过滤:db.test.find({m:{$exist:ture}})稀疏:优化分析方法,学习系统如何处理请求游标返回游标类型(BasicCursor或BtreeCursor)nscanned扫描文档数n返回文档数millis耗时(毫秒)indexBoundsusedindexhint强制使用索引db.test.find({"age":20}).hint({"name":1,"age":1})//.hint(name_1_age_1)profile设置日志级别记录慢查询Tips查询条件顺序自动调整,可以是带前缀的正则表达式类型命中索引(/^z/)为需要大量排序的键建立索引,避免将所有数据加载到内存中$ne,$nin不会使用索引索引type_id索引默认生成唯一字段单键索引值Valuedb.test.ensureIndex({x:1})多键索引值有多条记录,比如数组,嵌入文档db.test.insert({x:[1,2,3,4]})每个索引字段包含ns至多一个数组Y:{_id:1,a:[1,2],b:1,category:"Aarray"}and{_id:2,a:1,b:[1,2],category:"B数组"}N:{_id:3,a:[1,2],b:[1,2],category:"AB数组"}Query//Array查询数组包含:db.fruitshop.find({"fruits":"apple"})包含多个:db.fruitshop.find({"fruits":{"$all":["apple","banana"]}})精确匹配:db.fruitshop.find({"fruits":["apple","orange","pear"]})//下单与数量一致具体位置元素查询:db.fruitshop.find({"fruits.1":"orange"})queryarraylength:db.fruitshop.find({"fruits":{"$size":3}})//size不能和其他运算符一起使用,如'$gt'等返回固定长度:db.fruitshop.find({"fruits":{"$slice":2}})//第2个db.fruitshop.find({"fruits":{"$slice":-1}})//最后1个db.fruitshop.find({"fruits":{"$slice":[3,6]}})//第4到第7个,如果没有数据return[]//嵌入文档完全匹配:db.staff.find({"name":{"first":"joe","middle":"bush"}})//顺序且键值对的个数一致插入嵌入时在多个层中(约束):elemMatchdb.blogs.find({"comment":{"$elemMatch":{"author":"joe","score":{"$gte":3}}}})//嵌入文档匹配作者和评分条件wheredb.fruitshop.find({"$where":function(){}})//性能低下,每个文档转成javascrip将t对象放入执行复合索引多条件的函数中,从左开始执行{a:1,b:1,c:1}=>{a:1},{a:1,b:1},{atoright:1,b:1,c:1}db.test.ensureIndex({x:1,y:1})Expiration索引在一段时间后过期,并删除相应的数据(用户登录信息,存储logs)db.test.ensureIndex({time:1},{expireAfterSeconds:30})限制字段类型为ISODate或ISODate数组(数组中时间最小)不能为复合索引(不能指定两个过期时间)删除时间不准确(后台进程60s运行一次)可搜索全文索引字符串或字符串数??组//建立索引db.test.ensureIndex({title:"text"})db.test.ensureIndex({key1:"text",key2:"text"})//为多个字段建立全文索引db.test.ensureIndex({$**:"text"})//为所有字段建立全文索引//不指定字段名查找:db.test.find({"$text":{"$search":"coffee"}})//每个数据集只允许创建一个全文索引(针对一个、多个或所有字段)查找多个关键字(空格代表或操作):db.test.find({"$text":{"$search":"aabbcc"}})指定不包含单词(-代表没有操作):db.test.find({"$text"":{"$search":"aabb-cc"}})和关系操作:db.test.find({"$text":{"$search":"\"aa\"\"bb\"\"cc\""}})相似度查询:db.test.find({"$text":{"$search":"aabb"}},{"score:{"$meta":"textScore"}"})//score字段越高,相关性越高db.test.find({"$text":{"$search":"aabb"}},{"score":{"$meta":"textScore"}}).sort({"score":{"$meta":"textScore"}})//得分相关性排序//限制每个查询只指定一个$text。with$text,hint(mandatoryspecifiedindex)不起作用点二维索引用于存储和查找平面上的点db.test.ensureIndex({w:"2d"})//用经纬度来表示取值范围Longitude[-180,180]Latitude[-90,90]db.test。insert({w:[180,90]})//Query使用$near查询离一个点最近的点(默认返回100)db.test.find({"$near":[x,y]})db.test.find({w:{"$near":[x,y],"$maxDistance":"z"}})//限制返回的最远距离使用$geoWithin在一定形状内查询点矩形($box:[[x1,y1],[x2,y2]])db.test.find({w:{"$geoWithin:{"$box":[[0,0],[3,3]]}}"}})圆($center:[[x,y],r])db.test.find({w:{"$geoWithin":{"$center":[0,0],5}}})多边形($polygon:[[x1,y1],[x2,y2],..)db.test.find({w:{"$geoWithin":{"$polygon":[[0,0],[0,1],[2,5],[6,1]}}})使用$geoNear查询返回最大距离和平均距离等数据相关扩展:《地理位置索引的实现原理》2DsphereindexFor在球体上存储和查找点db.test.ensureIndex({key:"2dsphere"})
