【发布时间】:2018-11-13 21:56:33
【问题描述】:
我开始使用 MongoDB API,因为我们使用的是 Azure Cosmos DB。 在 MongoDB 中使用 $near 的示例,基本结构为 {key:"A": localtion:{type:"Point", coordinates:[1,2]}},效果很好。问题是当我需要使用位置数组时。
我正在尝试执行此查询而没有结果。我做错了什么?
db.places.insert( {
id:1,
name: "AAAAAAAAAAA",
locals:[
{
location: { type: "Point", coordinates: [-73.9928, 40.7191 ] },
}
],
category: "Parks"
} );
db.places.insert( {
id:2,
name: "BBBBBBBBBBB",
locals:[
{
location: { type: "Point", coordinates: [-73.9928, 40.7193 ] },
}
],
category: "Parks"
} );
db.places.insert( {
id:3,
name: "CCCCCCCCCCCCC",
locals:[
{
location: { type: "Point", coordinates: [ -73.9375, 40.8303 ] },
}
],
category: "Parks"
} );
//Create index
db.places.createIndex({ "locals.location" : "2dsphere" })
//Query without result
db.places.find(
{
"locals.location":
{ $near:
{
$geometry: { type: "Point", coordinates: [ -73.9375, 40.8303 ] },
$minDistance: 1000,
$maxDistance: 5000
}
}
}
)
这些地方有很多当地人,这就是为什么我可以在里面放很多东西。
我希望有人可以帮助我。
干杯。
【问题讨论】:
-
您正在使用 CosmosDB?如果是这样,那么我认为这行不通。 MongoDB 有一种支持聚合框架的方式,但这与 CosmosDB 的“兼容层”不兼容。这是两种完全不同的产品,尽管营销炒作声称并非如此。
-
另见How to query $near in CosmosDB via mongoDB protocol。至于“带有位置的子文档数组”,我不推荐它。您的示例很简单(至少对于 MongoDB),但即使是不受支持的
$geoNear也只能从子文档中识别出最多 一个 匹配项。请参阅$geoNear matching nearest array。 -
您好,我的回答对您有帮助吗?
-
谢谢@NeilLunn,你是对的。
标签: mongodb geolocation azure-cosmosdb geojson 2dsphere