使用数组参数进行地理空间查询
Geospatial querying with Array parameter
有可能吗?
我需要 运行 查询我的 collection 多个参数,例如数组。它可以是坐标或 GeoJSON 数组。没关系。
例如
coordinates: [ [3.45855, 18.154265], [4.13515, 22.181822] ]
GeoJSON: [
{"type":"Point", "coordinates":[3.45855, 18.154265]},
{"type":"Point", "coordinates":[4.13515, 22.181822]},
{"type":"Point", "coordinates":[5.45833, 24.154888]}
]
我知道 I cannot use $or
operator 地理空间查询。
但我也不能使用 $in 运算符。如果我的 collection 是如下示例文档,我如何使用上述数组之一查询我的 collection?
{
"_id" : ObjectId("54db485b72273a96985dafb7"),
"firstname" : "John",
"lastname" : "Doe",
"location" : {
"geometry" : {
"type" : "Point",
"coordinates" : [
40.937066,
29.326202
]
}
}
}
编辑:
实际上,我需要在数组查询中使用 $near 和 $maxDistance 运算符。我可以像下面这样正确地找到给定的只有一个 Point
参数的结果。有效;
{'location.geometry':
{
$near:
{
$geometry: GeoJSON[0],
$maxDistance: 500
}
}
}
但我需要如下查询;
{'location.geometry':
{
$near:
{
$geometry: { $in: GeoJSON },
$maxDistance: 500
}
}
}
我已经使用 async javascript library 解决了这个问题,如下所示;
var totalCount = 0
index = 0;
async.whilst(
function(){
return index < GeoJSON.length
},
function(callback) { // main function
var geoQuery = myCollection.find();
geoQuery.where({
'location.geometry': {
$near: {
$geometry: GeoJSON[index],
$maxDistance: 500
}
}
})
.count(function(err, count){
if(err) callback(err);
else {
totalCount += count;
index++;
callback(null)
}
})
},
function (err){ // callback function
res.send({
error: err,
count: totalCount
})
}
);
有可能吗? 我需要 运行 查询我的 collection 多个参数,例如数组。它可以是坐标或 GeoJSON 数组。没关系。
例如
coordinates: [ [3.45855, 18.154265], [4.13515, 22.181822] ]
GeoJSON: [
{"type":"Point", "coordinates":[3.45855, 18.154265]},
{"type":"Point", "coordinates":[4.13515, 22.181822]},
{"type":"Point", "coordinates":[5.45833, 24.154888]}
]
我知道 I cannot use $or
operator 地理空间查询。
但我也不能使用 $in 运算符。如果我的 collection 是如下示例文档,我如何使用上述数组之一查询我的 collection?
{
"_id" : ObjectId("54db485b72273a96985dafb7"),
"firstname" : "John",
"lastname" : "Doe",
"location" : {
"geometry" : {
"type" : "Point",
"coordinates" : [
40.937066,
29.326202
]
}
}
}
编辑:
实际上,我需要在数组查询中使用 $near 和 $maxDistance 运算符。我可以像下面这样正确地找到给定的只有一个 Point
参数的结果。有效;
{'location.geometry':
{
$near:
{
$geometry: GeoJSON[0],
$maxDistance: 500
}
}
}
但我需要如下查询;
{'location.geometry':
{
$near:
{
$geometry: { $in: GeoJSON },
$maxDistance: 500
}
}
}
我已经使用 async javascript library 解决了这个问题,如下所示;
var totalCount = 0
index = 0;
async.whilst(
function(){
return index < GeoJSON.length
},
function(callback) { // main function
var geoQuery = myCollection.find();
geoQuery.where({
'location.geometry': {
$near: {
$geometry: GeoJSON[index],
$maxDistance: 500
}
}
})
.count(function(err, count){
if(err) callback(err);
else {
totalCount += count;
index++;
callback(null)
}
})
},
function (err){ // callback function
res.send({
error: err,
count: totalCount
})
}
);