微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

春季数据MongoDB地理空间距离

如何解决春季数据MongoDB地理空间距离

我试图通过使用Spring Data Mongo GeoSpatial找到距离以及位置。

遵循此https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.geo-near

GeoResults<VenueWithdisField> = template.query(Venue.class) 
.as(VenueWithdisField.class)                            
.near(NearQuery.near(new GeoJsonPoint(-73.99,40.73),KILOMETERS))
.all();

我尝试过

@Data
@NoArgsConstructor
@AllArgsConstructor
public class RestaurantWithdisField {
   private Restaurant restaurant;
   private Number dis;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "restaurants")
public class Restaurant {
   @Id
   private String id;
   private String name;
   @GeoSpatialIndexed(name = "location",type = GeoSpatialIndexType.GEO_2DSPHERE)
   private GeoJsonPoint location;
}

public GeoResults<RestaurantWithdisField> findRestaurantsNear(GeoJsonPoint point,distance distance) {
    final NearQuery nearQuery = NearQuery.near(point)
            .maxdistance(distance)
            .spherical(true);
    return mongoTemplate.query(Restaurant.class)
            .as(RestaurantWithdisField.class)
            .near(nearQuery)
            .all();
}

但是结果是我得到了以下内容。如果我没有设置目标类型,而只是收集域类型,那么我会得到除距离以外的所有其他值。

Restaurant - RestaurantWithdisField(restaurant=null,dis=0.12914248082237584
Restaurant - RestaurantWithdisField(restaurant=null,dis=0.19842138954997746)
Restaurant - RestaurantWithdisField(restaurant=null,dis=0.20019522190348576)

有人可以帮助我为什么我无法获取域类型值,或者应该怎么办? 谢谢

解决方法

由于结果restaurant中的值与目标实体属性不匹配,因此映射无法解析RestaurantWithDisField中的Document

您可能想在这里使用继承而不是合成,并让RestaurantWithDisField扩展Restaurant,提供您自己的转换器或仅使用Restaurant并依靠GeoResults持有列表的GeoResult中已经包含Distance以及实际的映射域类型-几乎与您使用RestaurantWithDisField进行建模相同。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。