Spring-data mongodb 地理查询

4

我正在使用spring-data与mongodb进行通信,我正在寻找一种执行地理查询的方法,该方法将通过给定的地理点从数据库中检索附近的个人资料,我的查询要求如下:

1)按距离限制
2)按返回的个人资料数量限制
3)根据个人资料文档中的时间字段进行附加搜索
4)能够包含/排除检索到的文档中的字段

起初我使用了mongoTemplate.geoNear方法...

   Query criteria = new Query(Criteria.where("time").gte("some_date")); 
   criteria.fields().exclude("friends");
    NearQuery query = NearQuery.near(point).maxDistance(maxDistance)
            .num(limit).query(criteria);
    GeoResults<Profile> result = mongoTemplate
            .geoNear(query, Profile.class);<br/>

但是后来我发现mongo不支持在geoNear查询中包括/排除字段。
因此,我正在考虑使用Near查询,似乎在spring-data中执行那种“复杂”查询的唯一方法是使用@Query注释,在这种类型的查询中是否有一种支持要求2、3的方式?还有其他方法吗?
谢谢


你找到任何解决方案了吗? - gabby
Assaf - 你得到解决方案了吗?请帮帮我。谢谢,Neha。 - user5268786
这里有什么新闻吗?我该如何排除字段? - Fritz
1个回答

1
你的问题实际上已经包含了你所需的所有答案。你只需要使用query.addCriteria将它们组合起来。
1:使用半径为圆形 2. 使用Query.limit(count) 3. 使用Query.where(“date”)。gte() 4. 使用fields来投影结果 给你
    Point point = new Point(6.12343, 3.121211);
    Distance distance = new Distance(100, Metrics.KILOMETERS);
    Circle circle = new Circle(point, distance);
    Integer resultLimit = 20; //Limit to 20 records
    Criteria geoCriteria = Criteria.where("position").withinSphere(circle);
    Criteria timeCriteria = Criteria.where("time").gte(new Date());
    Query query = Query.query(geoCriteria);
    query.addCriteria(timeCriteria);
    query.fields().exclude("friends");
    mongoTemplate.find(query, Profile.class);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接