您的位置:首页 > 数据库 > Mongodb

使用mongodb地理位置索引查询,延时索引

2020-07-18 05:24 1126 查看

下面使用的mongdb图形化界面工具Robo 3T 1.3.1


数据库需要有对应的地理位置索引字段

查看索引

loc字段为一个长度为2的数组,代表当前数据的地理位置X, Y 坐标

索引类型: 2dsphere (地理位置索引)

应用场景推荐: 根据用户坐标查找附近店铺或商家

在Java中通过给定的位置坐标,查询出数据库20公里范围内的数据

Aggregation aggregation = Aggregation.newAggregation(
Aggregation.geoNear(
NearQuery.near(new Point(longitude, latitude))  //坐标数据
.maxDistance(20, Metrics.KILOMETERS)  //查找范围(半径),单位公里
.inKilometers()
.spherical(true), "distance"),

Aggregation.match(Criteria.where("status").is(1)),
Aggregation.match(Criteria.where("category_id").is(categoryId)),
Aggregation.match(Criteria.where("name").regex(".*?" + name + ".*", "i")),  //模糊查询,匹配正则表达式
Aggregation.skip(Long.valueOf((page - 1) * rows)),
Aggregation.limit(rows)
/**
* @param aggregation       :封装的查询条件
* @param "db_gpo_sales"    :索要查询的目标数据空
* @param DbGpoSales.class  :封装查询结果的实体类
* @return                  :所有范围内的数据
*/
AggregationResults<DbGpoSales> results = mongoTemplate.aggregate(aggregation, "db_gpo_sales", DbGpoSales.class);

数据库添加延时索引创建

右键点击Indexes



在高级选项里勾选延时索引,设置超时时间(单位:秒)

应用场景推荐: 用户下单,超过半小时未支付,自动取消订单


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: