Spring mongoDB GeoQueries

时间:2015-05-06 22:08:06

标签: spring spring-data-mongodb

您好我对spring很熟悉但不熟悉mongoDB 我有一个名为couponCoordinates

的GeoJsonPoint属性的简单类

我还使用以下方法创建了一个接口

public List<Coupon> findByCouponCoordinatesNear(Point point ,Distance distance);

死亡时出现以下错误

planner returned error: unable to find index for $geoNear query

我知道我能够手动添加索引,如果我这样做,实际上查询效果很好。但我有以下问题:

  1. 使用GeoJsonPoint是一种好习惯 2.有没有办法按代码添加索引 3.它与@CompoundIndexes注释相关
  2. 感谢

    更新

    以下是创建的查询:

      

    main] o.s.data.mongodb.core.MongoTemplate:在集合中插入包含字段的DBObject:[_ class,_id,businessName,businessDescription,couponName,couponDescription,couponWhatsappNumber,couponAddress,couponCoordinates,category,active]:coupon   2015-05-08 10:00:48.921 DEBUG 12228 --- [main] o.s.data.mongodb.core.MongoDbUtils:获取Mongo数据库名称= [cuponza]   2015-05-08 10:00:49.389 DEBUG 12228 --- [main] osdata.mongodb.core.MongoTemplate:插入包含字段的DBObject:[_ class,_id,businessName,businessDescription,couponName,couponDescription,couponWhatsappNumber,couponAddress,couponCoordinates ,category,active]收藏:优惠券   2015-05-08 10:00:49.390 DEBUG 12228 --- [main] o.s.data.mongodb.core.MongoDbUtils:获取Mongo数据库名称= [cuponza]   2015-05-08 10:00:49.411 DEBUG 12228 --- [main] osdmrquery.MongoQueryCreator:创建查询查询:{“couponCoordinates”:{“$ nearSphere”:{$ java:Point [x = 6.213700,y = -75.576700]},“$ maxDistance”:1.567855942887398E-4}},字段:null,排序:null   2015-05-08 10:00:49.416 DEBUG 12228 --- [main] osdata.mongodb.core.MongoTemplate:find using query:{“couponCoordinates”:{“$ nearSphere”:{“x”:6.2137,“ y“: - 75.5767},”$ maxDistance“:1.567855942887398E-4}} fields:null for class:class com.datasol.cuponza.model.Coupon in collection:coupon   2015-05-08 10:00:49.417 DEBUG 12228 --- [main] o.s.data.mongodb.core.MongoDbUtils:获取Mongo数据库名称= [cuponza]   2015-05-08 10:00:49.436 DEBUG 12228 --- [main] osdmrquery。 MongoQueryCreator:创建查询查询:{“couponCoordinates”:{“$ nearSphere”:{$ java:Point [x = 6.213700 ,y = -75.576700]},“$ maxDistance”:0.002351783914331097}},字段:null,排序:null   2015-05-08 10:00:49.438 DEBUG 12228 --- [main] osdata.mongodb.core.MongoTemplate:find using query:{“couponCoordinates”:{“$ nearSphere”:{“x”:6.2137,“ y“: - 75.5767},”$ maxDistance“:0.002351783914331097}} fields:null for class:class com.datasol.cuponza.model.Coupon in collection:coupon   2015-05-08 10:00:49.438 DEBUG 12228 --- [main] o.s.data.mongodb.core.MongoDbUtils:获取Mongo数据库名称= [cuponza]   2015-05-08 10:00:49.443 DEBUG 12228 --- [main] osdata.mongodb.core.MongoTemplate:find using query:{} fields:null for class:class com.datasol.cuponza.model.Coupon in收藏:优惠券   2015-05-08 10:00:49.443 DEBUG 12228 --- [main] o.s.data.mongodb.core.MongoDbUtils:获取Mongo数据库名称= [cuponza]   清理存储库   2015-05-08 10:00:49.446 DEBUG 12228 --- [main] o.s.data.mongodb.core.MongoDbUtils:获取Mongo数据库名称= [cuponza]   2015-05-08 10:00:49.447 DEBUG 12228 --- [main] o.s.data.mongodb.core.MongoTemplate:在集合中使用查询:{}删除:优惠券。

    更新II 有一个注释应该做到这一点 我将它放在我的优惠券类(具有@Document注释)中,就像这样

    @GeoSpatialIndexed(name="GeoIndex_2d")
    private Point couponCoordinates;
    

    但未创建索引 好像MongoPersistentEntityIndexCreator类没有把它拿起来

    更新II 这是POJO

    @Document
    public class Coupon {
    
    @Id
    private String couponId;
    
    private String businessName;
    
    private String businessDescription;
    
    private String couponName;
    
    private String couponDescription;
    
    private String couponWhatsappNumber;
    
    private String couponAddress;
    
    @GeoSpatialIndexed(name="GeoIndex_2d")
    private Point couponCoordinates;
    
    private CategoryEnum category;
    
    @TextIndexed
    private Set<String> keywords;
    
    private Boolean active;
    
    public Coupon (){}
    
    
    //getters and setters
    

    这是界面

    public interface CouponRepository extends MongoRepository<Coupon, String> {
    
    public List<Coupon> findByCouponCoordinatesNear(Point point ,Distance distance);
    

0 个答案:

没有答案