Geospatial index querying doesn't work on Azure CosmosDB (DocumentDB) using the Mongo API

江枫思渺然 提交于 2019-12-11 14:46:09

问题


I have an Azure CosmosDB with a collection restaurants where a field geoJSON specifies the location of a restaurant in geoJSON format. I am using the MongoDB API to access this.

When I log into the DB using the mongo shell, I am able to see all the documents using db.restaurants.find(). I created a 2dsphere geospatial index using db.restaurants.createIndex( {geoJSON: "2dsphere"}).

output:

{
    "_t" : "CreateIndexesResponse",
    "ok" : 1,
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 3,
    "numIndexesAfter" : 4
}

However, upon running db.restaurants.getIndexes(), I still see only three indexes. This question says that indexing is not allowed. Is this still the case?

Furthermore, there seems to be an index called

{
            "v" : 1,
            "key" : {
                    "DocumentDBDefaultIndex" : "2dsphere"
            },
            "name" : "DocumentDBDefaultIndex_2dsphere",
            "ns" : "<redacted>"
}

,but executing a geospatial query returns nothing.

db.restautants.find({DocumentDBDefaultIndex: {$near: {$geometry: {type: "Point", coordinates: [24.9430111, 60.166608]}, $maxDistance: 1000}}})

How can I execute geospatial queries against this index using the mongo shell? Is this a bug in CosmosDB?


回答1:


According to your description, I checked this issue on my side. For a quick way, I use MongoChef with the Azure Cosmos DB. Based on my test, db.brucetest1.createIndex({loc:"2dsphere"}) could not be applied to the collection. Moreover, I found DocumentDB automatically creates a 2dsphere index on the location field DocumentDBDefaultIndex, then I drop my documents, and insert the documents as follows:

db.brucetest.insertMany([
   {DocumentDBDefaultIndex : { type: "Point", coordinates: [ -73.97, 40.77 ] },name: "Central Park",category : "Parks"},
   {DocumentDBDefaultIndex : { type: "Point", coordinates: [ -73.88, 40.78 ] },name: "La Guardia Airport",category : "Airport"}
])

With the following query, I could encounter your issue:

db.brucetest.find({DocumentDBDefaultIndex:{$near:{$geometry:{type:"Point",coordinates:[-73.88, 40.78]}}}})

Based on the similar issue your mentioned, I assumed that the creating/updating index and Geospatial indexes querying features have not been implemented in the MongoDB Compatibility layer of Azure CosmosDB. Moreover, you could add your feedback here or wait for these features released.




回答2:


Geospatial IS currently supported with the MongoDB API. Please see the Geospatial operators section here.



来源:https://stackoverflow.com/questions/43937937/geospatial-index-querying-doesnt-work-on-azure-cosmosdb-documentdb-using-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!