使用圆作为区域进行地理查询以匹配 MongoDB 中至少一个 MultiPoint 对象的点

Geo-query using a circle as area to match at least one of the points of MultiPoint object in MongoDB

我在 Mongo 的 entities 集合中有以下文档(location.coords 的 2dsphere 索引已到位):

> db.entities.find({},{location: 1}).pretty()
{
    "_id" : {
        "id" : "en3",
        "type" : "t",
        "servicePath" : "/"
    },
    "location" : {
        "attrName" : "position",
        "coords" : {
            "type" : "MultiPoint",
            "coordinates" : [
                [
                    -3.691944,
                    40.418889
                ],
                [
                    4.691944,
                    45.418889
                ]
            ]
        }
    }
}

据我检查,$geoWithin 仅在几何包含多点的 所有点 时才匹配,例如:

> db.entities.find({"location.coords": { $geoWithin: { $centerSphere: [ [ -3.691944, 40.418889 ], 0.002118976612776644 ] } } })
// Small circle centered at first point, but without covering the second point: it doesn't matchh

> db.entities.find({"location.coords": { $geoWithin: { $centerSphere: [ [ -3.691944, 40.418889 ], 2 ] } } })
// Big circle centered at first point covering also the second point: it matches

但是,如果至少有一个点 MultiPoint 匹配,我想要一个匹配查询。我已经阅读了 $geoIntersects 运算符。我尝试在查询中将 $geoWithin 替换为 $geoIntersect,但它不起作用:

> db.entities.find({"location.coords": { $geoIntersects: { $centerSphere: [ [ -3.691944, 40.418889 ], 0.002118976612776644 ] } } })
error: {
    "$err" : "Can't canonicalize query: BadValue bad geo query",
    "code" : 17287
}

看了$geoIntersects运算符,好像只能用于多边形或者多边形,没有提到圆。我不知道我是否遗漏了什么,因为$geoWithin$geoIntersects之间的这个"asymmetry"似乎有点奇怪......

因此,有没有什么方法可以使用圆作为区域来匹配 MultiPoint 对象的至少一个点来进行地理查询?

我想我在最后找到了答案。可以使用 $near 运算符完成,方法如下:

db.entities.find({"location.coords": { $near: { $geometry: { type: "Point", "coordinates": [ -3.691944, 40.418889 ] }, $maxDistance: 0.5 } }})