1

我有一个地址字段类型为 Location (KeystoneJS 地址 + 地理坐标)的模型。据我了解,该字段中的地理坐标被索引为 2dSphere。

但是,如果我尝试使用 MongooseJS near 函数进行查询,它会返回错误:

var point = [3.2642048999999815241,50.830799100000090124];
Challenge.model.find(
    {status: 1}
)
.where('location')
.near({
    center: point, 
    maxDistance: maxDistance, 
    spherical: true
})
.populate('waypoints')
.exec(function(err, challenges) {
    if(err) return res.apiError('1000', Errors['1000']);
    return res.apiResponse({
        challenges: challenges
    });
});

返回:

MongoError: 找不到任何特殊索引: 2d (needs index), 2dsphere (needs index), for: { status: 1, location: { $nearSphere: [ 3.264204899999982, 50.83079910000009 ], $maxDistance: 1000 } }

我不明白为什么它会为此引发错误。

4

1 回答 1

3

似乎是 MongooseJS 中的一个错误。如果您使用 MongoDB 查询符号,它可以工作:

Challenge.model.find(
        {
         status: 1, 
         "location.geo": {$near: { $geometry:{ type: "Point", coordinates: location }, $maxDistance: maxDistance}}},
         "-__v -updatedOn -createdOn -slug")
.populate('waypoints')
.exec(function(err, challenges) {
    if(err) return res.apiError('1000', Errors['1000']);
    return res.apiResponse({
        challenges: challenges
    });
});
于 2014-06-17T08:59:02.667 回答