首页 > 解决方案 > Azure CosmosDb MongoAPI nearSphere return nothing

问题描述

I'm using CosmosDB with MongoDb API and I'd like to use $nearSphere to find Documents.

Here is the exemple of one document I have in my collection "locations":

{
    "_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]
    }
}

I would like to find the Document with location the nearest from [26, 27]. When I execute the following request, it returns nothing. However, this same command works fine we a MongoDB database : It returns the documents order by distance with point with coordonates [26, 27].

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

Do you know how I can make it work for Azure CosmosDB?

Thank you in advance.

标签: mongodbazure-cosmosdbazure-cosmosdb-mongoapi

解决方案


看起来我必须location.coordinates在查询中指定 Azure CosmosDB。这是工作查询:

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

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


推荐阅读