【问题标题】:MongoDB find $near in object arrayMongoDB 在对象数组中找到 $near
【发布时间】: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


【解决方案1】:

根据我对您的样本数据的测试,也得到了与您相同的空结果。据我所知,CosmosDB 仅支持 MongoDB API 的一个子集,并将请求转换为 CosmosDB SQL 等效项。 CosmosDB 有一些不同的行为和结果。但是 CosmosDB 有责任改进他们对 MongoDB 的仿真。

当然,您可以添加反馈 here 以提交您的要求,或者如果您想要完整的 MongoDB 功能支持,请考虑在 Azure 上使用 MongoDB Atlas

【讨论】:

  • 嗨,谢谢你的回答,我正在了解有关 CosmosDB 的更多信息,并向 azure 反馈发送电子邮件。我需要使用任何 CosmosDB api 找到替代方案。喜乐儿。
  • @PaulEscarcenaSantana cosmos db sql api怎么样?您可以使用存储过程或 udf 来实现您的要求。
  • @PaulEscarcena 当然,你可以标记是否回答,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-30
  • 2019-11-13
  • 1970-01-01
相关资源
最近更新 更多