猫鼬:在单个聚合中使用多个$ geoNear查询

时间:2020-01-26 09:34:44

标签: node.js mongodb mongodb-geospatial

我的模式有点像:

...
...
pointA: GeoJSON point
pointB: GeoJSON point
...
...

我正在传递两个参数,一个地理点(x)和一个地理点数组(y),并且只想返回那些符合以下条件的结果:

-> xpointA之间的距离不应大于1000m。

->数组y的任何一点都应位于pointB的100m之内。

不幸的是,似乎MongoDB不支持将多个$ geoNear查询一起管道传输

以下代码

Collection
.aggregate([
            {
                $geoNear: {
                    distanceField: "calculatedDistance",
                    spherical: true,
                    maxDistance: 1000,
                    near: x,
                    key: "pointA.coordinates"
                }
            },
            {
                $or: [
                         {
                            $geoNear: {
                                spherical: true,
                                maxDistance: 100,
                                key: "pointB.coordinates",
                                near: {
                                    type: "Point",
                                    coordinates: y[0]
                                },
                                distanceField: "calculatedDistanceB"
                            }
                         },
                         ...
                         ...
                         ...
                         ...
                         {
                            $geoNear: {
                                spherical: true,
                                maxDistance: 100,
                                key: "pointB.coordinates",
                                near: {
                                    type: "Point",
                                    coordinates: y[i]
                                },
                                distanceField: "calculatedDistanceB"
                            }
                         }
                     ]
            }
        ])

显示错误$geoNear is only valid as the first stage in a pipeline.

如何在单个查询中执行多个地理空间检查?

0 个答案:

没有答案