mongodb 地理位置处理

我只记录我用到的部分,没有完整分析mongodb对地理位置的支持 

 

1. 使用mongodb 3.2.6版,mongodb-driver 3.2.2版

  

2. 使用GeoSpatial索引:

    

use dbName
db.collectionName.ensureIndex({"location": "2dsphere"})

 

 

3. 查询一定距离范围内的记录:

         

// 查询fieldA不等于not_equal_value,status等于1,距离范围为100-2000米之内的第11-20行记录
double longitude = 120.21533;
double latitude = 30.253708;
int start = 10;
int limit = 20;
Bson query = Filters.and(Filters.ne("fieldA", "not_equal_value"), 
				Filters.eq("status", 1),
				Filters.nearSphere("location", new Point(new Position(longitude, latitude)), 2000d, 100d));
MongoCollection<Document> collection = getCollection("dbName", "collectionName");
FindIterable<Document> findIter = collection.find(query);
findIter = findIter.skip(start);
findIter = findIter.limit(size);
MongoCursor<Document> cursor = findIter.iterator();
Document document = null;
while (cursor.hasNext()) {
	document = cursor.next();
	// do with document
}
 

 

4. 查询距离最近的n条记录

 

// 查询status等于1,距离最近的10条记录
db.runCommand({geoNear:"collectionName", near:[120.21533, 30.253708], distanceMultiplier:6378137, num:10, spherical:true, query:{"status":1}})
 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值