地理空间索引查询不适用于 Azure CosmosDB (DocumentDB) 使用 Mongo API
Geospatial index querying doesn't work on Azure CosmosDB (DocumentDB) using the Mongo API
我有一个带有集合 restaurants
的 Azure CosmosDB,其中一个字段 geoJSON
以 geoJSON 格式指定餐厅的位置。我正在使用 MongoDB API 来访问它。
当我使用 mongo shell 登录数据库时,我可以使用 db.restaurants.find()
查看所有文档。我使用 db.restaurants.createIndex( {geoJSON: "2dsphere"})
创建了一个 2dsphere 地理空间索引。
输出:
{
"_t" : "CreateIndexesResponse",
"ok" : 1,
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 3,
"numIndexesAfter" : 4
}
然而,在 运行 db.restaurants.getIndexes()
上,我仍然只看到三个索引。 问题说索引是不允许的。现在还是这样吗?
此外,似乎有一个名为
的索引
{
"v" : 1,
"key" : {
"DocumentDBDefaultIndex" : "2dsphere"
},
"name" : "DocumentDBDefaultIndex_2dsphere",
"ns" : "<redacted>"
}
,但执行地理空间查询 returns 什么都没有。
db.restautants.find({DocumentDBDefaultIndex: {$near: {$geometry: {type: "Point", coordinates: [24.9430111, 60.166608]}, $maxDistance: 1000}}})
如何使用 mongo shell 对该索引执行地理空间查询?这是 CosmosDB 中的错误吗?
根据你的描述,我这边查了这个问题。为了快速,我将 MongoChef 与 Azure Cosmos DB 结合使用。根据我的测试,db.brucetest1.createIndex({loc:"2dsphere"})
无法应用于集合。此外,我发现 DocumentDB 会自动在位置字段 DocumentDBDefaultIndex
上创建一个 2dsphere 索引,然后我删除我的文档,并按如下方式插入文档:
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"}
])
通过以下查询,我可能会遇到您的问题:
db.brucetest.find({DocumentDBDefaultIndex:{$near:{$geometry:{type:"Point",coordinates:[-73.88, 40.78]}}}})
基于类似 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或等待这些功能发布。
MongoDBAPI 当前支持地理空间 IS。请参阅 Geospatial operators section here.
我有一个带有集合 restaurants
的 Azure CosmosDB,其中一个字段 geoJSON
以 geoJSON 格式指定餐厅的位置。我正在使用 MongoDB API 来访问它。
当我使用 mongo shell 登录数据库时,我可以使用 db.restaurants.find()
查看所有文档。我使用 db.restaurants.createIndex( {geoJSON: "2dsphere"})
创建了一个 2dsphere 地理空间索引。
输出:
{
"_t" : "CreateIndexesResponse",
"ok" : 1,
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 3,
"numIndexesAfter" : 4
}
然而,在 运行 db.restaurants.getIndexes()
上,我仍然只看到三个索引。
此外,似乎有一个名为
的索引{
"v" : 1,
"key" : {
"DocumentDBDefaultIndex" : "2dsphere"
},
"name" : "DocumentDBDefaultIndex_2dsphere",
"ns" : "<redacted>"
}
,但执行地理空间查询 returns 什么都没有。
db.restautants.find({DocumentDBDefaultIndex: {$near: {$geometry: {type: "Point", coordinates: [24.9430111, 60.166608]}, $maxDistance: 1000}}})
如何使用 mongo shell 对该索引执行地理空间查询?这是 CosmosDB 中的错误吗?
根据你的描述,我这边查了这个问题。为了快速,我将 MongoChef 与 Azure Cosmos DB 结合使用。根据我的测试,db.brucetest1.createIndex({loc:"2dsphere"})
无法应用于集合。此外,我发现 DocumentDB 会自动在位置字段 DocumentDBDefaultIndex
上创建一个 2dsphere 索引,然后我删除我的文档,并按如下方式插入文档:
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"}
])
通过以下查询,我可能会遇到您的问题:
db.brucetest.find({DocumentDBDefaultIndex:{$near:{$geometry:{type:"Point",coordinates:[-73.88, 40.78]}}}})
基于类似
MongoDBAPI 当前支持地理空间 IS。请参阅 Geospatial operators section here.