【发布时间】:2015-12-12 10:29:26
【问题描述】:
我将以下文档存储在 MongoDB 3.0.5 中
{
"_id" : ObjectId("55f3a6ae0907233b5e9e7da0"),
"user" : {
"id" : "2d5b093ec8a3",
"regions" : [
{
"id" : NumberLong(1442365443852),
"name" : "penta",
"type" : "Polygon",
"center" : {
"lat" : -12.1254159880008100,
"lng" : -77.0316830277442930
},
"zoom" : 17.0000000000000000,
"coordinates" : [
[
-77.0322804898023610,
-12.1271067552781560
],
[
-77.0336792618036270,
-12.1255133434450870
],
[
-77.0326449349522590,
-12.1239143495252150
],
[
-77.0300991833209990,
-12.1238251884504540
],
[
-77.0299865305423740,
-12.1262000752832540
],
[
-77.0322804898023610,
-12.1271067552781560
]
]
},
{
"id" : NumberLong(1442366496200),
"name" : "triangle",
"type" : "Polygon",
"center" : {
"lat" : -12.1254749913046230,
"lng" : -77.0316598936915400
},
"zoom" : 17.0000000000000000,
"coordinates" : [
[
-77.0313568040728570,
-12.1266573492018090
],
[
-77.0325788855552670,
-12.1246968022373030
],
[
-77.0300653204321860,
-12.1246233756874440
],
[
-77.0313568040728570,
-12.1266573492018090
]
]
}
]
}
}
表示存储为多边形的谷歌地图区域数组。我正在尝试用几种替代方法来查询它们,但似乎都没有,所以我想知道地理空间 MongoDB 查询是否适用于数组。
我的 Java 代码是:DBCursor docs = getCollection().find(search);
当几何上我期望匹配时,以下所有 6 个“搜索”查询均不返回任何结果:
-
{ "user.regions" : { "$geoIntersects" : { "$geometry" : { "type" : "Polygon" , "coordinates" : [ [ [ -77.02877718955278 , -12.123750122669545] , [ - 77.03457042574883,-12.123750122669545],[-77.03457042574883,-12.12736341792724],[-77.02877718955278,-12.12736341792724],[-77.02877718955278,-12.123750122669545]]]}}}}
LI> -
{ "user.regions.0" : { "$geoIntersects" : { "$geometry" : { "type" : "Polygon" , "coordinates" : [ [ [ -77.02877718955278 , -12.123750122669545] , [-77.03457042574883,-12.123750122669545],[-77.03457042574883,-12.12736341792724],[-77.02877718955278,-12.12736341792724],[-77.02877718955278,-12.123750122669545]]]}}}}
LI> -
{ "user.regions.0.center" : { "$geoIntersects" : { "$geometry" : { "type" : "Polygon" , "coordinates" : [ [ [ -77.02877718955278 , -12.123750122669545 ],[-77.03457042574883,-12.123750122669545],[-77.03457042574883,-12.12736341792724],[-77.02877718955278,-12.12736341792724],[-77.02877718955278,-12.123750122669545]]]}}}}
LI> -
{ "user.regions" : { "$geoWithin" : { "$geometry" : { "type" : "Polygon" , "coordinates" : [ [ [ -77.02877718955278 , -12.123750122669545] , [ - 77.03457042574883,-12.123750122669545],[-77.03457042574883,-12.12736341792724],[-77.02877718955278,-12.12736341792724],[-77.02877718955278,-12.123750122669545]]]}}}}
LI> -
{ "user.regions.0" : { "$geoWithin" : { "$geometry" : { "type" : "Polygon" , "coordinates" : [ [ [ -77.02877718955278 , -12.123750122669545] , [-77.03457042574883,-12.123750122669545],[-77.03457042574883,-12.12736341792724],[-77.02877718955278,-12.12736341792724],[-77.02877718955278,-12.123750122669545]]]}}}}
LI> -
{ "user.regions.0.center" : { "$geoWithin" : { "$geometry" : { "type" : "Polygon" , "coordinates" : [ [ [ -77.02877718955278 , -12.123750122669545 ],[-77.03457042574883,-12.123750122669545],[-77.03457042574883,-12.12736341792724],[-77.02877718955278,-12.12736341792724],[-77.02877718955278,-12.123750122669545]]]}}}}
LI>
我认为我尊重 MongoDB 中的 long/lat 顺序,我在我的多边形中重复结束点,我添加了 type="Polygon" 并且在中心点的情况下它位于 lat/lng 参数下。但没有结果。
我没有创建任何索引,我认为我匹配参数 geoWithin (http://docs.mongodb.org/manual/reference/operator/query/geoWithin/) 和 geoIntercepts (http://docs.mongodb.org/manual/reference/operator/query/geoIntersects/) 的语法
{
<location field>: {
$geoWithin: {
$geometry: {
type: <"Polygon" or "MultiPolygon"> ,
coordinates: [ <coordinates> ]
}
}
}
}
{
<location field>: {
$geoIntersects: {
$geometry: {
type: "<GeoJSON object type>" ,
coordinates: [ <coordinates> ]
}
}
}
}
关于“位置字段”,我按照此处所述的数组说明进行操作http://docs.mongodb.org/manual/tutorial/query-documents/
但是没有什么能让查询得到任何结果。我用谷歌搜索了同样的案例,我只能从 2013 年找到这个未解决的问题:How do I perform a find using $geoIntersects / 2dsphere in an array subfield?
所以,除非我犯了一个常见错误,否则我想知道是否可以对数组参数进行地理空间查询?
有什么建议吗?
谢谢
【问题讨论】:
-
我尝试的另一件事是将我的用例视为此处定义的 GeometryCollection docs.mongodb.org/manual/reference/geojson/…,将 type: "GeometryCollection" 参数添加到我的用户参数并按几何图形更改区域。没有索引。通过使用位置字段 user.geometries 再次查询没有结果。
标签: mongodb mongodb-query geospatial