MongoDB 地理空间查询无法找到 $geoNear 查询的索引'

MongoDB Geospatial Query unable to find index for $geoNear query'

我正在尝试根据当前经纬度和距离使用 mongo GeoSpatialIndexed

查找附近的位置

但我遇到了异常:

There was an unexpected error (type=Internal Server Error, status=500).
Query failed with error code 291 and error message 'error processing query: ns=test.store limit=2Tree: GEONEAR field=address.location maxdist=0.00313571 isNearSphere=1 Sort: {} Proj: {} planner returned error :: caused by :: unable to find index for $geoNear query' on server localhost:27017; nested exception is com.mongodb.MongoQueryException: Query failed with error code 291 and error message 'error processing query: ns=test.store limit=2Tree: GEONEAR field=address.location maxdist=0.00313571 isNearSphere=1 Sort: {} Proj: {} planner returned error :: caused by :: unable to find index for $geoNear query' on server localhost:27017
org.springframework.data.mongodb.UncategorizedMongoDbException: Query failed with error code 291 and error message 'error processing query: ns=test.store limit=2Tree: GEONEAR  field=address.location maxdist=0.00313571 isNearSphere=1
Sort: {}
Proj: {}
 planner returned error :: caused by :: unable to find index for $geoNear query' on server localhost:27017; nested exception is com.mongodb.MongoQueryException: Query failed with error code 291 and error message 'error processing query: ns=test.store limit=2Tree: GEONEAR  field=address.location maxdist=0.00313571 isNearSphere=1
Sort: {}
Proj: {}
 planner returned error :: caused by :: unable to find index for $geoNear query' on server localhost:27017
    at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:133)
    at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2863)
    at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2788)
    at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2531)

文件详情如下:

{
    "_id" : ObjectId("5ef9e8404fd7e821fd49be32"),
    "name" : "Location Name",
    "address" : {
        "street" : "Location street",
        "city" : "Location city",
        "zip" : "987890",
        "location" : {
            "x" : -71.073203,
            "y" : 43.031873
        }
    },
    "_class" : "a.b.cStore"
}

我的 Pojo class 如下:


@Data
@Document
public class Store {

    @Id
    private final String id;

    private final String name;

    private final Address address;

    public Store(String name, Address address) {
        id = null;
        this.name = name;
        this.address = address;
    }
    
}

AND


@Data
@Document
public class Address {

    private final String street;
    private final String city;
    private final String zip;

    private final @GeoSpatialIndexed
    Point location;
}

我的存储库中有以下代码

@Repository
public interface StoreRepository extends MongoRepository<Store, String> {

    Page<Store> findByAddressLocationNear(Point location, Distance distance, Pageable pageable);
}

对于单个项目,我可以使用 followin 存储库方法获取对象

Store findByAddressLocationXAndAddressLocationY(Double x, Double y);

不幸的是我无法让它工作,我已经遵循了这个文档https://docs.spring.io/spring-data/mongodb/docs/1.2.0.RELEASE/reference/html/mongo.repositories.html

我在创建索引时使用了错误的参数

错误

db.store.createIndex({"location": "2d"});

正确

根据我的 pojo class,应该是

db.store.createIndex({"address.location": "2d"});

魔法开始发挥作用,一切照旧。