Mongodb的索引--学习笔记(未完)

全文索引

建立方法:
--在articles集合的key字段上创建全文索引
db.articles.ensureIndex({key:"text"})
--在articles集合的key_1,key_2字段上创建全文索引
db.articles.ensuereIndex({key_1:"text",key_2:"text"})
--在articles集合的所有字段上创建全文索引
db.articles.ensuereIndex({"$**":"text"})

--在articles集合中的article字段上创建全文索引
> db.articles.ensureIndex({"article":"text"});
{
        "createdCollectionAutomatically" : true,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
> 
--插入演示数据
db.articles.insert({"article":"aa bb"})
db.articles.insert({"article":"aa bb cc "})
db.articles.insert({"article":"aa bb cc dd"})
db.articles.insert({"article":"aa bb cc rr"})


使用全文索引查询
--查询包含aa 或 bb 或 cc的文档
db.articles.find({$text:{$search:"aa bb cc"}})
--查询包含aa 或 bb 但不包含 cc的文档
db.articles.find({$text:{$search:"aa bb -cc"}})
--查询包含aa 、bb 且包含cc的文档
db.articles.find({$text:{$search:"\"aa\" \"bb\" \"cc\""}})


全文索引的相似度
$meta操作符:{score:{$meta:"textScore"}}
写在查询条件后面可以返回查询结果的相似度
经常与sort一起使用

--查询含有"aa bb"字段的文本,并返回查询结果的相似度
> db.articles.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}});
{ "_id" : ObjectId("5798ad223206da9bc38b2370"), "article" : "aa bb cc dd", "score" : 1.25 }
{ "_id" : ObjectId("5798ad293206da9bc38b2371"), "article" : "aa bb cc rr", "score" : 1.25 }
{ "_id" : ObjectId("5798ad1b3206da9bc38b236f"), "article" : "aa bb cc ", "score" : 1.3333333333333333 }
{ "_id" : ObjectId("5798ad143206da9bc38b236e"), "article" : "aa bb", "score" : 1.5 }
{ "_id" : ObjectId("5798ae383206da9bc38b2372"), "article" : "aa", "score" : 1.1 }
--根据相似度排序
> db.articles.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}});
{ "_id" : ObjectId("5798ad143206da9bc38b236e"), "article" : "aa bb", "score" : 1.5 }
{ "_id" : ObjectId("5798ad1b3206da9bc38b236f"), "article" : "aa bb cc ", "score" : 1.3333333333333333 }
{ "_id" : ObjectId("5798ad223206da9bc38b2370"), "article" : "aa bb cc dd", "score" : 1.25 }
{ "_id" : ObjectId("5798ad293206da9bc38b2371"), "article" : "aa bb cc rr", "score" : 1.25 }
{ "_id" : ObjectId("5798ae383206da9bc38b2372"), "article" : "aa", "score" : 1.1 }
> 

全文索引使用的限制(版本2.6.5)
1.每次查询,只能指定一个$text查询(一个集合只能创建一个全文索引)
2.$text查询不能出现在$nor查询中
3.查询中如果包含$text,hint不在起作用
4.当前Mongodb的全文索引还不支持中文

索引属性

创建索引的格式:
db.collection.ensureIndex({indexValue},{indexProperty})
其中indexProperty比较重要的有:
1.名字,name指定:
  db.collection.ensureIndex({},{name:""})
2.唯一性,unique指定:
  db.collection.ensureIndex({},{unique:true/false})
3.稀疏性,sparse指定
  稀疏性指是否为文档中不存在的字段创建索引
  db.collection.ensureIndex({},{sparse:true/false})
4.是否定时删除,expireAfterSeconds指定:
  TTL 过期索引

地理位置索引

地理位置索引
    将一些点的位置存储到Mongodb中,创建索引后,可以按照位置来
查找其他点

子分类
2d索引:用于存储和查找平面上的点
2dsphere索引:用于存储和查找球面上的点

查找方式
1.查找距离某个点一定距离内的点
2.查找包含在某区域内的点

2d索引详解
1.创建索引方式
  db.collection.ensureIndex({w:"2d"})

2.位置表示方式
  经纬度[经度,纬度]

3.取值范围
  经度[-180,180] 纬度[-90,90]

4.插入位置数据
  >db.location.insert({w:[1,1]})
  >db.location.insert({w:[1,2]})
  >db.location.insert({w:[5,6]})
  >db.location.insert({w:[200,1]}) #Mongodb会直接报错,经度超出范围
  >db.location.insert({w:[180,100]}) #纬度超出范围,Mongodb并没有报错,但是后期查询会出现不可预知的错误。
  >db.location.insert({w:[79,76]})

5.查询方式
    $near查询 查询距离某个点的最近点
    >db.location.find({w:{$near:[1,1]}}) #默认返回100个
    --可通过$maxDistance来限制查找的最远距离
    >db.location.find({w:{$near:[1,1],$maxDistance:10}}) #限制查找最远距离为10

    $geoWithin查询 查询某个形状内的点
    形状有三种表示方式
    1)$box:矩形,使用{$box:[[<x1>,<y1>],[<x2>,<y2>]]}表示,内部是两个坐标,第一个代表左边界,第二个代表右边界。
    >db.collection.find({w:{$geoWithin:{$box:[[0,0],[3,3]]}}})
    2)$center:圆形,使用{$center:[[<x1>,<y1>],r]}表示,内部是圆心位置和半径。
    >db.collection.find({w:{$geoWithin:{$center:[[0,0],5]}}})
    3)$polygon:多边形,使用{$polygon:[[<x1>,<y1>],[<x2>,<y2>],[<x3>,<y3>]]}表示,内部是坐标点,坐标点围成一个多边形。
    >db.collection.find({w:{$geoWithin:{$polygon:[[0,0],[0,1],[2,5],[6,1]]}}})

  geoNear查询 是对$near查询的补充
  db.runCommand({
         getNear:<collection>,
         near:[x,y],
         minDistance:(对2d索引无效)
         maxDistance:
         num:
     })

 

posted @ 2016-08-17 15:11  PoleStar  阅读(234)  评论(0编辑  收藏  举报