繁体   English   中英

如何使用geoNear查询MongoDB?

[英]How to query MongoDB using geoNear?

我正在尝试使用mongodb使用聚合函数geoNear和near来定位距离,具体取决于经度和纬度。

`router.get('/devops' , (req,res,next) => {
   /*
   Devops.find({})
   .then((alldevops) => {
      res.send(alldevops);
   })
   */
  Devops.aggregate().near({
   near: [parseFloat(req.query.lng), parseFloat(req.query.lat)],
   maxDistance: 100000,
   spherical: true,
   distanceField: "dist.calculated"
  }).then((alldevops)=>{
     res.send(alldevops);
  });

});`

我收到一条错误消息:

MongoError:当查询GeoJSON点时,geo near仅接受一个参数。 找到的额外字段:$ max距离:100000.0

首先,您的架构应类似于下面指定的jeo.json格式

{
  "other_key":"other_key_type"

 // this is important
  "geometry": {
    "type": "Point",
    "coordinates": [Number] // it is [latitude, longitude], yes it is reverse way
  }

}

现在,您具有该架构,可以使用该架构将数据插入数据库中

现在,您可以通过以下示例在数据上应用地理附近概念

YourModal.aggregate([
    {
      $geoNear: {
        near: { type: 'Point', coordinates: [16.51194, 80.735627] },
        distanceField: 'dist.calculated',
        includeLocs: 'dist.location',
        spherical: true
      }
    },
    {
      $project: {
        dist: 1
      }
    }
  ])

在这里,您将获得许多选择,但我已经从给定坐标中提取了唯一的距离。 我认为这将为从数据库实现geoNear查询提供一个简短的思路

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM