【问题标题】:MongoDB, SpringData, findByPositionNear more parametersMongoDB、SpringData、findByPositionNear 更多参数
【发布时间】:2026-01-12 02:20:07
【问题描述】:

您好有以下实现:

public class Store {
   private double[] position;
   private String category;
}

而且这种搜索商店的方法就在附近

public List<Store> fundByLatLong(double latitude, double longitude, Double km) {

mongoTemplate.indexOps(Store.class).ensureIndex(new GeospatialIndex("position"));

    Point point= new Point(latitude, longitude);

    List<Store> stores= this.storeRepository.findByPositionNear(ponto , new Distance(km, Metrics.KILOMETERS));

    return stores;
}

但我需要按职位和类别搜索。如何为查找添加参数类别?

【问题讨论】:

    标签: mongodb spring-data


    【解决方案1】:

    这就是你要找的吗?

    interface StoreRepository extends Repository<Store, Long> {
    
      List<Store> findByCategoryAndPositionNear(String category, 
                                                Point point, Distance distance);
    }
    

    reference documentation 中有关此类内容的更多信息。

    【讨论】:

    • 是的,但是这个接口在 SpringData 中不是原生的 SpringData 原生接口是 findByPositionNear(Point p, Distance d) SpringData 在运行时实现这个接口。我如何告诉 SpringData 实现我需要的接口?
    • “非本地”是什么意思?上面显示的接口定义开箱即用。您不能告诉 Spring Data 实现任意接口。您的界面需要遵守 Spring Data 支持的功能或自己实现 repo 的部分。所有这些都记录在reference documentation