Reputation: 369
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?
Upvotes: 0
Views: 1735
Reputation: 2176
Geospatial IS currently supported with the MongoDB API. Please see the Geospatial operators section here.
Upvotes: 0
Reputation: 18465
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.
Upvotes: 1