mongo-查附近

192 阅读1分钟

mongodb支持地理位置保存(GeoJsonPoint:mongodb提供专门用来提供经纬度封装的对象)

//1 查询自身的坐标
        Query query = new Query(Criteria.where("userId").is(id));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        //2 做距离的半径
        Distance dt = new Distance(distance / 1000, Metrics.KILOMETERS);
        Circle circle = new Circle(userLocation.getLocation(), dt);
        //3 根据自身的坐标和指定的距离半径做附近人的搜索
        Query query2 = new Query(Criteria.where("location").withinSphere(circle));