【问题标题】:Error finding distance between two points using $geoNear in MongoDB在 MongoDB 中使用 $geoNear 查找两点之间的距离时出错
【发布时间】:2019-09-03 21:50:39
【问题描述】:

我正在尝试使用 $geoNear mongoDB 函数查找两点之间的距离并收到以下错误:

MongoError:错误处理查询:ns=Driveoo.vehiclesTree:GEONEAR
field=location maxdist=10000 isNearSphere=0↵Sort: {}↵Proj: {}↵ planner 返回错误:无法找到 $geoNear queryCar Search 的索引 失败

请在下面找到我的代码:

exports.getNearestVehicles = function (req, res, next) {
    Vehicle.find({
      location: {
       $near: {
        $maxDistance: 10000,
        $geometry: {
         type: "Point",
         coordinates: [req.query.lat, req.query.long]
        }
       }
      }
     }).then( results => {
        return responseHandler.resHandler(true,JSON.stringify(results,0,2),"Car Response", res, 200);
    })
    .catch(error=> {
        return responseHandler.resHandler(false,null,error+"Car Search Failed", res, 200);
    });
}

谁能解决我哪里做错了?

【问题讨论】:

    标签: node.js database mongodb mongoose geolocation


    【解决方案1】:

    您是否在查询之前创建了二维球体索引?

    您可以按如下方式创建索引

    db.createIndex({'location','2dSphere'})

    同样在上述查询中,您没有使用 geoNear,$geoNear 通常与聚合一起使用。

    【讨论】:

    • 嗨@sachin。您能否举例说明如何使用 $geoNear 和聚合来查找两点之间的距离?是的,我在查询之前创建了二维球体索引
    猜你喜欢
    • 2019-09-16
    • 1970-01-01
    • 2023-01-20
    • 2012-09-01
    • 2016-02-25
    • 1970-01-01
    相关资源
    最近更新 更多