我正在尝试使用文档字段中定义的最大距离制定内部条件,但我没有发现任何相关内容 . 我刚刚找到了如何使用数字设置最大距离,但没有使用字段名称 .

我的文件中有这些字段:

  • id:string

  • basePosition:GeoJsonPoint

  • nearEnabled:布尔值

  • nearMaxDistance:整数

我使用MongoTemplate来进行查询:这是我的查询代码

public GeoResults<MyEntity> searchQuery(double latitude, double longitude, Integer maxDistanceKm, Long skip, Integer limit) {
GeoJsonPoint point = new GeoJsonPoint(longitude, latitude);

// Simple fields criteria
Query mongoQuery = new Query().addCriteria(
        // I have one simple where here but i removed it for the post
);

mongoQuery.addCriteria(
    new Criteria().orOperator(
        new Criteria().andOperator(
            Criteria.where("nearEnabled").is(true)
            //TODO need to add my withinSphere here, with field basePosition as center point and field nearMaxDistance as limit radius
        )
        // I have another and operator with 2 where here but i removed it for the post
    )
);

long skipPrimitive = skip != null ? skip : 0;
NearQuery nearQuery = NearQuery.near(point).query(mongoQuery).skip(skipPrimitive).spherical(true);

if(maxDistanceKm != null) nearQuery = nearQuery.maxDistance(maxDistanceKm, Metrics.KILOMETERS);
if(limit != null) nearQuery.num(skipPrimitive + limit);

GeospatialIndex geoIndex = new GeospatialIndex("basePosition").typed(GeoSpatialIndexType.GEO_2DSPHERE);
mongoTemplate.indexOps(ProfileEntity.class).ensureIndex(geoIndex);

return mongoTemplate.geoNear(nearQuery, MyEntity.class);}

正如我们在代码中看到的,我有一些其他的标准,分页和基于inSphere的我的字段必须在or>和运算符中 . 我在nearQuery上也有一个全局maxDistance .

我在查询中搜索一种方法 . 否则我可以在我的服务上应用Java List过滤器,但它将在分页后运行,它将打破每页结果数量的概念,直接在查询中执行此操作将防止此问题 .