mongodb java 地理_Java MongoDB 地理空间查询

@SuppressWarnings({ "unchecked", "deprecation" })

@Override

public String[] queryAggregationCommand(Point point) {

String collectionName = mongoTemplate.getCollectionName(LocationInfo.class);

Query basicQuery = new BasicQuery("{actor : 1}");

NearQuery nearQuery = NearQuery

.near(point)/*以point为坐标点*/

.inKilometers()/*单位公里*/

.query(basicQuery)/*查询条件*/

.num(Integer.MAX_VALUE)/*返回记录数*/

.spherical(true)/*球面*/

.maxDistance(500);/*最大距离*/

Assert.notNull(nearQuery, "NearQuery must not be null!");

Assert.hasText(collectionName, "Entity class must not be null!");

DBObject geoNearFields = nearQuery.toDBObject();

geoNearFields.put("distanceField", "results.dis");

DBObject geoNear = new BasicDBObject("$geoNear", geoNearFields);

DBObject projectFields = new BasicDBObject();

projectFields.put("_id", 0);

projectFields.put("pushChannelId", 1);

String cmpJson = "{$cmp : ['$distance', '$results.dis']}";

DBObject cmpFields = (DBObject) JSON.parse(cmpJson);

projectFields.put("cmp", cmpFields);

DBObject project = new BasicDBObject("$project", projectFields);

String matchJson = "{$match : {cmp : {$gte : 0}}}";

DBObject match = (DBObject) JSON.parse(matchJson);

DBObject limit = new BasicDBObject("$limit", 10000);

/*

* Note:

* 1.使用$goNear只能在管道处理的开始第一个阶段进行

* 2.必须指定distanceField,该字段用来决定是否包含距离字段

* 3.$gonNear和geoNear命令比较相似,但是也有一些不同:distanceField在$geoNear中是必选的,

* 而在geoNear中是可选的;includeLocs在$geoNear中是string类型,而在geoNear中是boolen类型

* */

List pipeLine = Lists.newArrayList();

pipeLine.add(geoNear);

pipeLine.add(project);

pipeLine.add(match);

pipeLine.add(limit);

/*logger.debug("queryAggregationCommand:{}", pipeLine.toString());*/

AggregationOutput output = mongoTemplate.getCollection(collectionName).aggregate(pipeLine);

List list = (List) output.getCommandResult().get("result");

/*字段筛选*/

List channels = Lists.transform(list, new Function() {

@Override

public String apply(DBObject input) {

return (String)input.get("pushChannelId");

}

});

String[] result = new String[channels.size()];

result = channels.toArray(result);

return result;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值