Java + MongoDB操作地理位置
准备工作(文章中MongoDB代码,有用//注释的,在运行代码时,删除这些注释!!!)
1.下载MongoDB服务器(Windows版) - mongodb-win32-x86_64-2012plus-4.2.8-signed.msi
下载地址: https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2012plus-4.2.8-signed.msi
2.下载界面软件 - studio-3t-x64.zip
下载地址: https://download.studio3t.com/studio-3t/windows/2020.5.0/studio-3t-x64.zip
3.可以远程连接MongoDB数据库
在MongoDB安装bin文件下配置:
找到mongod.cfg文件,修改net: bindIp: 0.0.0.0
同样,这里也可以修改端口: port: 27017
4.MongoDB基本语法
//如果数据库不存在则创建数据库,否则切换到指定数据库.
use DATABASE_NAME
//插入单个
db.test.insertOne(
{item: "canvas", qty: 100, tags: ["cotton"], size: {h: 28,w: 35.5, uom: "cm"}}
)
//插入多个
db.test.insertMany([
{item: "journal", qty: 25, tags: ["blank","red"], size: {h: 14, w: 21, uom: "cm"}},
{item: "mat", qty: 85, tags: ["gray"], size: {h: 27.9, w: 35.5, uom: "cm"}},
{item: "mousepad", qty: 25, tags: ["gel", "blue"], size: {h: 19, w: 22.85, nom: "cm"}}
])
//查询所有 select * from Table
db.students.find()
//或者
db.getCollection("students").find({})
//条件查询 select * from Table where item = "mat"
db.students.find({item: "mat"})
//条件查询之 "in" select * from Table where item IN ("","")
db.students.find({
item: {
$in: [
'mat','journal'
]
}
})
//条件查询之 "and" select * from Table where item = "" and qty > ?
db.students.find({
item: "mat",
qty: {
$gt: 30
}
})
//条件查询之 "or" select * from Table where item = "" or qty > ?
db.students.find(
{
$or:[
{
item: "mat"
},
{
qty:{
$gt: 30
}
}
]
}
)
//条件查询之 "or" 和 "and" 条件select * from Table where item = "" and (qty > ? or item LIKE "%p%")
db.students.find(
{
item: "mousepad",
$or:[
{
item: /s/
},
{
qty:{
$gt: 30
}
}
]
}
)
//更新单个文档 ... 等等
5.地理空间操作
//新建test表
在Studio 3T 界面 选中Collections右键,选择"Add Collection...."
//设置空间索引 - 验证是否设置成功: 选中Collections右键"Refresh All",然后点击"test"下拉菜单,出现"location_2dsphere"
db.test.ensureIndex( { location : "2dsphere" } )
//插入测试数据
db.test.insert({roleId:"12341234124",created:"2020-06-18T10:41:49.321+0000",nickname:"王富贵",photoLink:"头像",content:"你好",location:{type:"Point",coordinates:[105.754484701156,41.689607057699]}})
db.test.insert({roleId:"12312234124",created:"2020-06-19T10:41:49.321+0000",nickname:"王富贵",photoLink:"头像",content:"你好",location:{type:"Point",coordinates:[105.304045248031,41.783456183240]}})
db.test.insert({roleId:"12341234211",created:"2020-06-16T10:41:49.321+0000",nickname:"王富贵",photoLink:"头像",content:"你好",location:{type:"Point",coordinates:[105.084318685531,41.389027478812]}})
db.test.insert({roleId:"12341234121",created:"2020-06-15T10:41:49.321+0000",nickname:"王富贵",photoLink:"头像",content:"你好",location:{type:"Point",coordinates:[105.831388998031,41.285916385493]}})
db.test.insert({roleId:"12341221212",created:"2020-06-14T10:41:49.321+0000",nickname:"王富贵",photoLink:"头像",content:"你好",location:{type:"Point",coordinates:[106.128706502914,42.086868474465]}})
db.test.insert({roleId:"12431234214",created:"2020-06-12T10:41:49.321+0000",nickname:"王富贵",photoLink:"头像",content:"你好",location:{type:"Point",coordinates:[105.431074666976,42.009365053841]}})
db.test.insert({roleId:"21341234341",created:"2020-06-13T10:41:49.321+0000",nickname:"王富贵",photoLink:"头像",content:"你好",location:{type:"Point",coordinates:[104.705977010726,41.921549795110]}})
//查询某地理位置一定范围内测试数据的个数信息 coordinates是中心点,maxDistance是设置圆心半径,sort是按照什么进行排序(1:正序,-1倒序),limit()是要查询多少条"7"是查询符合范围中的7条
db.test.find({
location: {
$nearSphere: {
$geometry:{
type: "Point",
coordinates: [105.794621276855,41.869574065014]
},
$maxDistance: 200000
}
}
}).sort({"created": 1}).limit(7)
6. 使用Java进行实现上面测试数据的插入操作
// 两个实体类,一个实体类时业务实体类,另一个实体类用来组装地理位置信息"location"字段. 插入mongoTemplate.insert(对象);
package com.whale.doteonme.web.module.web.chatmodule.bean;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@Document(collection = "test")
public class PmNearbyChatMongoDB {//实体类 一
@ApiModelProperty(value = "roleId",required = false,dataType = "Object")
private Object roleId;
@ApiModelProperty(value = "昵称",required = false,dataType = "String")
private String nickname;
@ApiModelProperty(value = "头像",required = false,dataType = "String")
private String photoLink;
@ApiModelProperty(value = "创建时间",required = false,dataType = "String")
private String created;
@ApiModelProperty(value = "聊天内容",required = false,dataType = "String")
private String content;
@ApiModelProperty(value = "经纬度",required = false,dataType = "Object")
private Object location;
public Object getRoleId() {
return roleId;
}
public void setRoleId(Object roleId) {
this.roleId = roleId;
}
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
public Object getLocation() {
return location;
}
public void setLocation(Object location) {
this.location = location;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getPhotoLink() {
return photoLink;
}
public void setPhotoLink(String photoLink) {
this.photoLink = photoLink;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
package com.whale.doteonme.web.module.web.chatmodule.bean;
/**
* @author LiuQiang
* @description 地理位置对象
* @date 2020-06-22 13:52
*/
public class GeomArrayBean {//实体类 二
private String type;
private Double [] coordinates;
public GeomArrayBean(){
type = "Point";
}
public String getType(){
return type;
}
public void setType(String type){
this.type = type;
}
public Double [] getCoordinates(){
return coordinates;
}
public void setCoordinates(Double [] coordinates){
this.coordinates = coordinates;
}
}
public void insertPmNearbyChat(Long roleId, String content, float longitude, float latitude) {
PmNearbyChatMongo pmNearbyChatMongo = new PmNearbyChatMongo();
pmNearbyChatMongo.setRoleId(roleId);
pmNearbyChatMongo.setNickname("二丫");
pmNearbyChatMongo.setPhotoLink("www.baidu.com");
pmNearbyChatMongo.setCreated(new Date());
pmNearbyChatMongo.setContent(content);
double v = Double.parseDouble(String.valueOf(longitude));
double v1 = Double.parseDouble(String.valueOf(latitude));
GeomArrayBean geomArrayBean = new GeomArrayBean();
Double [] doubles = {v,v1};
geomArrayBean.setType(Constant.WorldChatPointType);
geomArrayBean.setCoordinates(doubles);
pmNearbyChatMongo.setLocation(geomArrayBean);
mongoTemplate.insert(pmNearbyChatMongo);//存储到MongoDB中
}
7.使用Java实现读取符合地理位置的数据
//就是将MongoDB的查询语句使用Java API表示.
public List<PmNearbyChatMongoDB> selectPmNearbyChatMongoDB(float longitude, float latitude) {
double v = Double.parseDouble(String.valueOf(longitude));
double v2 = Double.parseDouble(String.valueOf(latitude));
//附近聊天记录
BasicDBObject basicDBObject = new BasicDBObject("location",
new BasicDBObject("$nearSphere",
new BasicDBObject("$geometry",
new BasicDBObject("type","Point")
.append("coordinates",new double[] {v,v2}))
.append("$maxDistance",200000)));
BasicQuery basicQuery = new BasicQuery(String.valueOf(basicDBObject));
basicQuery.with(new Sort(Sort.Direction.ASC,"created"));//按照创建时间字段正序排列
basicQuery.limit(100);//限制数据数量
List<PmNearbyChatMongoDB> list = mongoTemplate.find(basicQuery, PmNearbyChatMongoDB.class, "test");//test指表名
return list;
}
package com.whale.doteonme.web.module.web.chatmodule.bean;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(value="mongoDb存储信息")
public class PmNearbyChatMongoDB {
@ApiModelProperty(value = "roleId",required = false,dataType = "Object")
private Object roleId;
@ApiModelProperty(value = "昵称",required = false,dataType = "String")
private String nickname;
@ApiModelProperty(value = "头像",required = false,dataType = "String")
private String photoLink;
@ApiModelProperty(value = "创建时间",required = false,dataType = "String")
private String created;
@ApiModelProperty(value = "聊天内容",required = false,dataType = "String")
private String content;
@ApiModelProperty(value = "经纬度",required = false,dataType = "Object")
private Object location;
public Object getRoleId() {
return roleId;
}
public void setRoleId(Object roleId) {
this.roleId = roleId;
}
public String getCreated() {
return created;
}
public void setCreated(String created) {
this.created = created;
}
public Object getLocation() {
return location;
}
public void setLocation(Object location) {
this.location = location;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getPhotoLink() {
return photoLink;
}
public void setPhotoLink(String photoLink) {
this.photoLink = photoLink;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
8. 解决问题
如果你有更好的解决方案,请给我留言,期待你的留言.
本文详细介绍了如何使用Java与MongoDB进行地理位置操作,包括MongoDB服务器与界面软件的下载、远程连接设置、基本语法的学习以及通过Java进行地理位置数据的插入和查询。通过实例展示了Java API在实现地理空间查询上的应用。
771

被折叠的 条评论
为什么被折叠?



