MongoDB 上的 MultiPolgyon 搜索
Posted
技术标签:
【中文标题】MongoDB 上的 MultiPolgyon 搜索【英文标题】:MultiPolgyon search on MongoDB 【发布时间】:2015-04-04 12:28:44 【问题描述】:我似乎无法弄清楚这一点,并希望得到任何帮助。 我正在使用 Node.js 使用 MultiPolygon 过滤器对 MongoDB 进行查询。我正在连接到本机驱动程序而不是使用 Mongoose,因为 Mongoose 似乎不支持 MultiPolygon(文档中不存在)。
我收到了格式错误的查询错误
GatheredListings.collection.find(
$and: [
'rentAmount': $gte: filterParameters.minPrice ,
'rentAmount': $lte: filterParameters.maxPrice ,
'availabilityDate': $gte: filterParameters.date ,
'propertyType': filterParameters.propertyType ,
'bedrooms': filterParameters.bedrooms ,
'location':
$geoWithin:
$geometry:
type : "MultiPolygon" ,
coordinates: [
geoArrayCollection
]
]
).toArray(function(err, properties)
if (err || !properties)
console.log('err ' + err);
callback("No properties found", null);
else
console.log('Number of properties retrieved: ' + properties.length);
callback(null, properties);
);`
错误是: `ERR MongoError:格式错误地理查询:$ geoWithin:$几何:类型: “的MultiPolygon”,坐标:[[[[43.77307711737606,-79.53517913818359],[43.79191518340848,-79.4476318359375],[43.75559702541283,-79.43887710571289],[ 43.7501411993079,-79.46514129638672],[43.7541091221655,-79.46943283081055],[43.75547303488856,-79.47406768798828],[43.75510106177428,-79.47715759277344],[43.75435710860915,-79.48093414306641],[43.75200119590339,-79.48282241821289],[43.74592499302,-79.48316574096678],[43.74282465186857 ,-79.49621200561523],[43.7404682852067,-79.49604034423828],[43.73873195568971,-79.49295043945312],[43.73525914559611,-79.49501037597655],[43.73538317799622,-79.50410842895508],[43.73290248118248,-79.51148986816406],[43.73947610307701,-79.51320648193359],[43.73649945803657, -79.52642440795898 ], [ 43.77307711737606, -79.53517913818359 ] ], [ [ 43.79203909839882, -79.4476318359375 ], [ 43.803809850899574, -7474, -7 560547],[43.76334592336985,-79.38712120056152],[43.76179622406369,-79.39501762390137],[43.73600333614323,-79.43381309509277],[43.79203909839882,-79.4476318359375]]]] P>
谢谢
【问题讨论】:
不确定这是否有助于进一步解决问题,但我正在连接到 MongoLabs 并使用 Mongoose 连接到 node-mongodb-native(据我了解,.collection 绕过 Mongoose 并直接进入节点- mongodb-native。 【参考方案1】:MultiPolygon 的格式不正确。你想要这个:
"type": "MultiPolygon",
"coordinates": [
[
[
[
43.77307711737606,
-79.5351791381836
],
[
43.79191518340848,
-79.4476318359375
],
[
43.75559702541283,
-79.43887710571289
],
[
43.7501411993079,
-79.46514129638672
],
[
43.7541091221655,
-79.46943283081055
],
[
43.75547303488856,
-79.47406768798828
],
[
43.75510106177428,
-79.47715759277344
],
[
43.75435710860915,
-79.4809341430664
],
[
43.75200119590339,
-79.48282241821289
],
[
43.74592499302,
-79.48316574096678
],
[
43.74282465186857,
-79.49621200561523
],
[
43.7404682852067,
-79.49604034423828
],
[
43.73873195568971,
-79.49295043945312
],
[
43.73525914559611,
-79.49501037597655
],
[
43.73538317799622,
-79.50410842895508
],
[
43.73290248118248,
-79.51148986816406
],
[
43.73947610307701,
-79.5132064819336
],
[
43.73649945803657,
-79.52642440795898
],
[
43.77307711737606,
-79.5351791381836
]
]
], // <--- missing this
[ // <--- missing this
[
[
43.79203909839882,
-79.4476318359375
],
[
43.80380985089954,
-79.39647674560547
],
[
43.76334592336985,
-79.38712120056152
],
[
43.76179622406369,
-79.39501762390137
],
[
43.73600333614323,
-79.43381309509277
],
[
43.79203909839882,
-79.4476318359375
]
]
]
]
但是你错过了我强调的数组的关闭和打开层。这些结构又大又嵌套,所以很难看清楚发生了什么;我建议使用jsonlint 和geojsonlint 之类的工具将我更正的结构与您之前的结构进行对比。这是我能够发现问题的唯一方法。
【讨论】:
谢谢,很好的回复:格式化,但这仍然不能解决问题。我实际上尝试了各种不同的格式选项。以上也不起作用:( 您可以发布您正在执行的完整查询吗?您使用的是哪个版本的 MongoDB?在 MongoDB 2.6.7 中,如果我将更正后的结构剪切并粘贴到$geoWithin
中,它可以正常工作。以上是关于MongoDB 上的 MultiPolgyon 搜索的主要内容,如果未能解决你的问题,请参考以下文章