【问题标题】:Alternative to $geoNear MongoDB$geoNear MongoDB 的替代品
【发布时间】:2021-01-12 07:44:35
【问题描述】:

我正在开发一个 API。我的一个端点向我返回 Spots 围绕由页面排序的 LONGITUDELATITUDE 坐标定义的点。我还发送了一个 PAGE_SIZE 用于返回一个数字或 Spots,并发送一个 MIN_DISTANCE 从何处开始作为最后一页的最后一项的页面。

我正在使用 $geoNear 键来执行此操作,这很方便,因为它还可以返回我用来进行分页的中心的距离。

遗憾的是,我现在将数据库托管在 azure COSMOS DB 上,似乎不支持 $geoNear

https://docs.microsoft.com/en-us/azure/cosmos-db/mongodb-feature-support-36#aggregation-stages

获得最相似结果的最佳选择是什么?有一堆或 key 听起来不错,但无法找出最佳选择:($geoWithin, $geoIntersects, $near, $nearSphere, $geometry)

https://docs.microsoft.com/en-us/azure/cosmos-db/mongodb-feature-support-36#geospatial-operators

Spots.aggregate
    ([
      {
        $geoNear: {
          near: {
            type: "Point",
            coordinates: [ LONGITUDE, LATITUDE ]
          },
          distanceField: "location.distance",
          spherical: true,
          minDistance: MIN_DISTANCE,
          maxDistance: DISTANCE * 1000,
          query: {
            $and: [
              ...
            ]
          }
        }
      },
      { $match: {} },
      { $limit : PAGE_SIZE},
      ...

谢谢:)

【问题讨论】:

  • 应该是$near$nearSphere

标签: node.js mongodb azure mongoose azure-cosmosdb-mongoapi


【解决方案1】:

我终于使用了 $nearSphere 并得到了几乎相同的结果,除了我将手动计算的距离。

Spots.aggregate(
    [
      {
        $match: {
          $and: [
            {
              location: {
                $nearSphere: {
                  $geometry: {
                      type : "Point",
                      coordinates : [ LONGITUDE, LATITUDE ]
                  },
                  $minDistance: MIN_DISTANCE,
                  $maxDistance: MIN_DISTANCE * 1000
                }
              }
            },
            ...
          ]
        }
      },
      { $limit : PAGE_SIZE},
      ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-11
    • 2015-01-29
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 2015-11-18
    • 1970-01-01
    相关资源
    最近更新 更多