【问题标题】:MongooseJS near not working with Keystone Location fieldMongooseJS 几乎无法使用 Keystone Location 字段
【发布时间】:2014-06-17 16:59:02
【问题描述】:

我有一个模型,其地址字段类型为 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: can't find any special indices: 2d (needs index), 2dsphere (needs index), for: { status: 1, location: { $nearSphere: [ 3.264204899999982, 50.83079910000009 ], $maxDistance: 1000 } }

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

【问题讨论】:

    标签: javascript mongodb mongoose keystonejs


    【解决方案1】:

    似乎是 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-07-04
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      • 2021-11-02
      • 2013-07-09
      相关资源
      最近更新 更多