MongoDB的3.x版本Java驱动相对2.x做了全新的设计,类库和使用方法上有很大区别。例如用Document替换BasicDBObject、通过Builders类构建Bson替代直接输入$命令等,本文整理了基于3.2版本的常用增删改查操作的使用方法。为了避免冗长的篇幅,分为增删改、查询、聚合、地理索引等几部分。

随着移动设备的普及,基于坐标和经纬度的位置查询变得越来越流行,例如查找离当前位置最近的N辆出租车。Mongodb专门针对这种查询建立了地理空间索引:2d和2dsphere索引。2d用于平面基于坐标的位置计算,2dsphere主要用于球体,比如地球,提供了基于弧度的位置计算。

import static com.mongodb.client.model.Indexes.geo2d;
import static com.mongodb.client.model.Indexes.geo2dsphere; import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import org.bson.Document; import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.geojson.LineString;
import com.mongodb.client.model.geojson.Point;
import com.mongodb.client.model.geojson.Polygon;
import com.mongodb.client.model.geojson.Position; public class GeospatialExamples { public static void main(String[] args) throws ParseException {
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("lesson"); GeospatialExamples client = new GeospatialExamples(database);
client.show();
mongoClient.close();
} private MongoDatabase database;
public GeospatialExamples(MongoDatabase database) {
this.database = database;
} public void show() {
MongoCollection<Document> mc = database.getCollection("people");
mc.drop(); Document doc1 = new Document("name", "tom").append("raid", Arrays.asList(10, 10)).append("gps", new Point(new Position(10, 10)));
Document doc2 = new Document("name", "jone").append("raid", Arrays.asList(10.1, 10)).append("gps", new Point(new Position(10.1, 10)));
Document doc3 = new Document("name", "john").append("raid", Arrays.asList(10, 10.1)).append("gps", new Point(new Position(10, 10.1)));
Document doc4 = new Document("name", "jack").append("raid", Arrays.asList(9.9, 10)).append("gps", new Point(new Position(9.9, 10)));
Document doc5 = new Document("name", "mary").append("raid", Arrays.asList(10, 9.9)).append("gps", new Point(new Position(10, 9.9)));
Document doc6 = new Document("name", "abby").append("raid", Arrays.asList(10.2, 10)).append("gps", new Point(new Position(10.2, 10)));
Document doc7 = new Document("name", "adam").append("raid", Arrays.asList(10.3, 10)).append("gps", new Point(new Position(10.3, 10)));
Document doc8 = new Document("name", "barry").append("raid", Arrays.asList(10.4, 10)).append("gps", new Point(new Position(10.4, 10)));
Document doc9 = new Document("name", "anne").append("raid", Arrays.asList(10.5, 10)).append("gps", new Point(new Position(10.5, 10)));
mc.insertMany(Arrays.asList(doc1, doc2, doc3, doc4, doc5, doc6, doc7, doc8, doc9)); mc.createIndex(geo2d("raid"));
mc.createIndex(geo2dsphere("gps")); //$geoWithin 匹配任意几何图形内搜索
FindIterable<Document> iterable = mc.find(Filters.geoWithin("raid", new Polygon(Arrays.asList(new Position(10.2, 10), new Position(10, 10.2), new Position(9.8, 10), new Position(10, 9.8), new Position(10.2, 10)))));
printResult("Filters.geoWithin raid", iterable); //$geoWithinBox 在以左下角和右上角坐标构成方形内搜索
iterable = mc.find(Filters.geoWithinBox("raid", 9.8, 9.8, 10.2, 10.2));
printResult("Filters.geoWithinBox raid", iterable); //$geoWithinPolygon 在多边形内搜索
List<Double> p1 = new ArrayList<>();
List<Double> p2 = new ArrayList<>();
List<Double> p3 = new ArrayList<>();
p1.add(10d);
p1.add(10d);
p2.add(10.1);
p2.add(10.16);
p3.add(10.2);
p3.add(10d);
List<List<Double>> polygon = Arrays.asList(p1, p2, p3);
iterable = mc.find(Filters.geoWithinPolygon("raid", polygon));
printResult("Filters.geoWithinPolygon raid", iterable); p2.clear();
p2.add(9.9);
p2.add(10.16);
p3.clear();
p3.add(9.8);
p3.add(10d);
polygon = Arrays.asList(p1, p2, p3);
iterable = mc.find(Filters.geoWithinPolygon("gps", polygon));
printResult("Filters.geoWithinPolygon gps", iterable); //$geoWithinCenter 在指定圆心和半径的圆形内搜索
iterable = mc.find(Filters.geoWithinCenter("raid", 10d, 10d, 0.25));
printResult("Filters.geoWithinCenter raid", iterable); //$geoWithinCenterSphere 在球体(地球)上指定圆心和弧度搜索, 例如搜索以[10,10]为中心500米内的文档, 参数为...10d, 10d, 0.5/6371
iterable = mc.find(Filters.geoWithinCenterSphere("gps", 10d, 10d, 11d/6371));
printResult("Filters.geoWithinCenterSphere gps", iterable); //$geoIntersects
iterable = mc.find(Filters.geoIntersects("gps", new LineString(Arrays.asList(new Position(10, 10.1), new Position(10.1, 10), new Position(10, 9.9)))));
printResult("Filters.geoIntersects gps", iterable); //$near
iterable = mc.find(Filters.near("gps", new Point(new Position(10, 10)), 20566d, 0d));
printResult("Filters.near gps", iterable); //$nearSphere
iterable = mc.find(Filters.nearSphere("gps", new Point(new Position(10, 10)), 20566d, 10d));
printResult("Filters.nearSphere gps", iterable);
} public void printResult(String doing, FindIterable<Document> iterable) {
System.out.println(doing);
iterable.forEach(new Block<Document>() {
public void apply(final Document document) {
System.out.println(document);
}
});
System.out.println("------------------------------------------------------");
System.out.println();
}
}

MongoDB-JAVA-Driver 3.2版本常用代码全整理(4) - 地理空间索引的更多相关文章

  1. MongoDB-JAVA-Driver 3.2版本常用代码全整理(3) - 聚合

    MongoDB的3.x版本Java驱动相对2.x做了全新的设计,类库和使用方法上有很大区别.例如用Document替换BasicDBObject.通过Builders类构建Bson替代直接输入$命令等 ...

  2. MongoDB-JAVA-Driver 3.2版本常用代码全整理(2) - 查询

    MongoDB的3.x版本Java驱动相对2.x做了全新的设计,类库和使用方法上有很大区别.例如用Document替换BasicDBObject.通过Builders类构建Bson替代直接输入$命令等 ...

  3. MongoDB-JAVA-Driver 3.2版本常用代码全整理(1) - 增删改

    MongoDB的3.x版本java驱动相对2.x做了全新的设计,类库和使用方法上有很大区别.例如用Document替换BasicDBObject.通过Builders类构建Bson替代直接输入$命令等 ...

  4. MongoDB Java Driver操作指南

    MongoDB为Java提供了非常丰富的API操作,相比关系型数据库,这种NoSQL本身的数据也有点面向对象的意思,所以对于Java来说,Mongo的数据结构更加友好. MongoDB在今年做了一次重 ...

  5. Mongodb Java Driver 参数配置解析

    要正确使用Mongodb Java Driver,MongoClientOptions参数配置对数据库访问的并发性能影响极大. connectionsPerHost:与目标数据库能够建立的最大conn ...

  6. 单元测试系列之十:Sonar 常用代码规则整理(二)

    摘要:帮助公司部署了一套sonar平台,经过一段时间运行,发现有一些问题出现频率很高,因此有必要将这些问题进行整理总结和分析,避免再次出现类似问题. 作者原创技术文章,转载请注明出处 ======== ...

  7. 单元测试系列之九:Sonar 常用代码规则整理(一)

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 摘要:公司部署了一套sonar,经过一段时间运行,发现有一些问题出现频率很高,因此有必要将这些问题进行整理总结和分 ...

  8. MongoDB Java Driver

    本文使用 Java 来描述对 Mongodb 的相关操作,数据库版本是 3.2.8,驱动版本为 3.2.2. 本文将讨论 如何连接MongoDB 文档的 CURD 操作 文档的上传和下载 1. 连接到 ...

  9. MongoDB Java Driver 3.4操作

    导入jar包 <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-dr ...

随机推荐

  1. nyoj 82 迷宫寻宝(二)

    http://acm.nyist.net/JudgeOnline/problem.php?pid=83 题目解法主要在于判断两线段是否相交,思路是穷举所有地图四周的点,其中每一个边界上的点和终点构成一 ...

  2. 高级搜索插件solis search在umbraco中的使用

    好久没有写关于umbraco的博客了,这段时间在研究solis search,感觉它太强大,好东西是需要分享的,所以写一篇简单的使用博客分享给个人umbraco爱好者. 简介 在了解solis sea ...

  3. [REP]AWS Regions and Availability Zones: the simplest explanation you will ever find around

    When it comes to Amazon Web Services, there are two concepts that are extremely important and spanni ...

  4. 【HOW】如何配置SharePoint传入/传出电子邮件设置

    SharePoint 2010的传入和传出邮件配置选项都较简单,但由于需要DNS及Exchange等服务器互相配合,所以要正确配置并不容易. 在微软的官方文档中详细说明了配置步骤:配置传入电子邮件:h ...

  5. XData -–无需开发、基于配置的数据库RESTful服务,可作为移动App和ExtJS、WPF/Silverlight、Ajax等应用的服务端

    XData -–无需开发.基于配置的数据库RESTful服务,可作为移动App和ExtJS.WPF/Silverlight.Ajax等应用的服务端   源起一个App项目,Web服务器就一台,已经装了 ...

  6. spring mvc使用@InitBinder 标签对表单数据绑定

    在SpringMVC中,bean中定义了Date,double等类型,如果没有做任何处理的话,日期以及double都无法绑定. 解决的办法就是使用spring mvc提供的@InitBinder标签 ...

  7. Unix 用gdb分析core dump文件

    产生core文件条件 用ulimit -c 指定core文件大小来开启core文件的生成,如:ulimit -c unlimited 用gdb分析core文件的条件 可执行程序在编译时,需加入-g参数 ...

  8. WebStorm 10.0.4注册码

    WebStorm 是JetBrains旗下的一款轻量级JavaScript 开发工具,软件的功能非常强大,具备复杂客户端和服务器端的开发能力,被广大中国JS开发者誉为“Web前端开发神器”.“最强大的 ...

  9. oracle 执行 delete user$ 误删所有用户信息后的数据恢复流程

    起因: 在oracle测试过程中,不小心执行了delete user$ 命令,导致oracle当前实例所有的用户信息丢失,包括sys用户. 第一次使用DUL工具数据恢复:失败 下载ParnassusD ...

  10. 如何安装NodeJS到阿里云Centos (64位版本V5-7)

    如何安装NodeJS到阿里云Centos (64位版本V5-7) (Centos与Red Hat® Enterprise Linux® / RHEL, Fedora属于一类) 1) 安装v0.10版 ...