Java + MongoDB 实现地理位置操作

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 &#
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值