mongdb索引

全文索引:

创建:db.collection.ensureIndex({"key":"text"})

使用:db.collection.find({$test:{$search:"aa"}})  

注:aa为要查找的内容

 全文索引相识度:

$meta操作符:{score:{$meta:"testscore"}}

写在查询条件后面可以返回返回结果的相识度,与sort()一起使用可以达到很好的效果。

例:db.collection.find({$test:{$search:"aa"}},{score:{$meta:"testscore"}}).sort({score:{$meta:"testscore"}})

全文索引使用限制:

每次查询只能使用一个$text查询,$text查询不能出现在$nor查询中,查询中如果包含了$text,hint不起作用,mongdb全文索引还不支持中文

索引属性:

名字,name指定:db.collection.ensureIndex({},{name:""})

唯一性,unique指定:db.collection.ensureIndex({},{unique:true/false})

稀疏性,sparse指定:db.collection.ensureIndex({},{sparse:true/false})

 是否定时删除,expireAfterSeconds指定:TTL,过期索引

地理位置索引 (例:打车)

子分类:

2d索引:平面地理位置索引,用于存储和查找平面上的点

创建方式:db.collection.ensureIndex({w:"2d"});位置表示方式:经纬度[经度,纬度],取值范围:经度[-180,180],纬度[-90,90];

查询方式:1.$near查询:查询距离某个点最近的点 2.$geoWithin查询:查询某个形状内的点,3.geoNear查询:使用runCommand命令进行查询

db.location.find({w:{$near:[1,1]}}) 默认查询最近的100个距离的点;db.location.find({w:{$near:[1,1],$maxDistance:10}}) 查询距离最近的十个点

形状的表示:  1.$box:矩形,使用{$box:[[<x1>,<y1>],[<x2>,<y2>]]} 2.$center:圆形,使用{$center:[[<x1>,<y1>],r]} 3.$polygon:多边形,使用{$polygon:[[<x1>,<y1>],[<x2>,<y2>],[<x3>,<y3>]]}  例:db.location.find({w:{$geoWithin:{$box:[0,0],[2,1]}}})

2dsphere索引:球面地理位置索引,用于存储和查找球面上的点

创建方式:db.collection.ensureIndex({w:"2dsphere"}) ;位置表示方式:GeoJSON:描述一个点,一条直线,多边形等形状;格式:{type:"",coordinates:[<coordinates>]}

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

posted @ 2018-11-01 23:24  金州恶汉拉文  阅读(141)  评论(0编辑  收藏  举报