【问题标题】:Azure CosmosDb MongoAPI nearSphere return nothingAzure Cosmos Db Mongo API nearSphere 不返回任何内容
【发布时间】:2021-06-15 02:45:35
【问题描述】:

我正在使用 CosmosDB 和 MongoDb API,我想使用 $nearSphere 来查找文档。

以下是我收藏的“位置”中的一份文档的示例:

{
    "_id": {
        "$oid": "60523b3dd72e4d4aa21f473b"
    },
    "data": [{
        "Indicateur": "pr50mm",
        "Value": 0,
        "score": 0,
        "Exposure": "low"
    }],
    "Domain": "EU-CORDEX",
    "GCMS": "ICHEC-EC-EARTH",
    "RCMS": "RACMO22E",
    "Scenario": "rcp85",
    "Horizon": "Medium (1941-1970)",
    "location": {
        "type": "Point",
        "coordinates": [26.32, 27.25]
    }
}

我想找到位置离 [26, 27] 最近的文档。 当我执行以下请求时,它什么也不返回。但是,对于 MongoDB 数据库,同样的命令也可以正常工作:它按距离与坐标点 [26, 27] 的距离顺序返回文档。

db.locations.find({
  'location': {
    $nearSphere: {
      $geometry: {
        type: 'Point',
        coordinates: [
          26, 27
        ]
      }
    }
  }
})

你知道我如何使它适用于 Azure CosmosDB 吗?

提前谢谢你。

【问题讨论】:

  • 我在我的地方试过了,我发现如果我使用变量[26.32, 27.25]执行查询,它可以返回文档,但是当我使用[26, 27]时,它什么也不返回,您能否检查一下您的 mongodb 数据库中是否存在 coordonates [26, 27]?
  • @Tiny-wa 我的结果和你一样。不,我没有 [26,27] 的任何文件。但是,我不希望它只返回具有精确位置匹配的文档,而是返回距 [26, 27] 最近的位置(在我的情况下是:[26.32, 27.25])。感谢您的帮助。
  • 感谢您的回复,如果您急于弄清楚这个功能,我想您可以试试这个。 {$nearSphere: {$geometry: {type : "Point",coordinates : [26.32, 27.25]}, $minDistance: , $maxDistance: }} 然后使用 $minDistance 和 $maxDistance可以得到最近的位置。但是这里仍然需要使用精确的坐标作为参数。
  • 顺便问一下,你添加了 db 的 2dsphere 索引了吗?文档说使用 $nearSphere 需要该索引,但是当我在没有索引的情况下执行查询时它没有返回错误。
  • @Tiny-wa,感谢您的帮助,我找到了解决方案:我必须在查询中指定 location.coordinates。此外,您不能在 Azure CosmosDB 上添加 2dsphere 索引

标签: mongodb azure-cosmosdb azure-cosmosdb-mongoapi


【解决方案1】:

对于 Azure CosmosDB,我必须在查询中指定 location.coordinates。这是工作查询:

db.locations.find({
  'location.coordinates': {
    $nearSphere: {
      $geometry: {
        type: 'Point',
        coordinates: [
          26, 27
        ]
      }
    }
  }
})

这个关于$nearSphere 的例子帮助了我:https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/cosmos-db/mongodb-feature-support.md

我希望这对其他人有帮助!

【讨论】:

    猜你喜欢
    • 2023-02-21
    • 1970-01-01
    • 2021-11-14
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多