mongodb java 地理位置_地理位置服务:MongoDB实现 - 使用样例

地理位置服务:MongoDB实现 - 使用样例

1 更新用户地理位置

客户端检测用户的地理位置,当变化大于500米时或每隔5分钟,向服务端发送地理位置。

dubbo - pojo

package com.tanhua.dubbo.server.pojo;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import org.bson.types.ObjectId;

import org.springframework.data.annotation.Id;

import org.springframework.data.mongodb.core.geo.GeoJsonPoint;

import org.springframework.data.mongodb.core.index.CompoundIndex;

import org.springframework.data.mongodb.core.index.Indexed;

import org.springframework.data.mongodb.core.mapping.Document;

@Data

@NoArgsConstructor

@AllArgsConstructor

@Document(collection = "user_location")

@CompoundIndex(name = "location_index", def = "{'location': '2dsphere'}")

public class UserLocation implements java.io.Serializable{

private static final long serialVersionUID = 4508868382007529970L;

@Id

private ObjectId id;

@Indexed

private Long userId; //用户id

private GeoJsonPoint location; //x:经度 y:纬度

private String address; //位置描述

private Long created; //创建时间

private Long updated; //更新时间

private Long lastUpdated; //上次更新时间

}

dubbo - 接口

package com.tanhua.dubbo.server.api;

public interface UserLocationApi {

/**

* 更新用户地理位置

*

* @return

*/

String updateUserLocation(Long userId, Double longitude, Double latitude, String address);

}

dubbo - 实现

package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;

import com.tanhua.dubbo.server.pojo.UserLocation;

import org.bson.types.ObjectId;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.core.geo.GeoJsonPoint;

import org.springframework.data.mongodb.core.query.Criteria;

import org.springframework.data.mongodb.core.query.Query;

import org.springframework.data.mongodb.core.query.Update;

@Service(version = "1.0.0")

public class UserLocationApiImpl implements UserLocationApi {

@Autowired

private MongoTemplate mongoTemplate;

@Override

public String updateUserLocation(Long userId, Double longitude, Double latitude, String address) {

UserLocation userLocation = new UserLocation();

userLocation.setAddress(address);

userLocation.setLocation(new GeoJsonPoint(longitude, latitude));

userLocation.setUserId(userId);

Query query = Query.query(Criteria.where("userId").is(userLocation.getUserId()));

UserLocation ul = this.mongoTemplate.findOne(query, UserLocation.class);

if (ul == null) {

//新增

userLocation.setId(ObjectId.get());

userLocation.setCreated(System.currentTimeMillis());

userLocation.setUpdated(userLocation.getCreated());

userLocation.setLastUpdated(userLocation.getCreated());

this.mongoTemplate.save(userLocation);

return userLo

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.
06-01

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值