1

我正在使用 MongoDB 和用于 mongoDB 的 Node.js 驱动程序开发 API。其中一条路线接收坐标并调用聚合管道,该管道返回按接近度和许多其他因素排序的结果。管道第一步的代码是:

{$geoNear:{
  near:{
     type: "Point",
     coordinates: [lat, long]
  },
  spherical:true,
  distanceField:"distance"
}},

这一直很好,直到突然之间,它不是。现在它适用于某些坐标,而对于其他坐标则给出错误MongoError: invalid argument in geo near query: type。它可以正常工作{lat:0, long:0},可以正常工作,可以{lat:40.7411, long:-73.9897}正常工作{lat:46.68758191106798, long:2.8106467331441767},但是会出现错误{lat:37.785834, long:-122.406417}and {lat:47.643628614308675, long:-122.34560056016635}。我能找到的唯一模式是它讨厌西海岸的坐标。我已经验证了 2d-sphere 索引仍然存在于数据中。我试过将球形设置为假。我什至尝试删除所有数据,即使使用空集合仍然会得到相同的行为。我真的很困惑,为什么对 geonear 的论点对某些观点有效,而对其他观点无效。

编辑:我现在已经注释掉了除了以下代码之外的所有内容,并且我仍然得到相同的行为,其中一些坐标起作用而其他坐标给出MongoError: invalid argument in geo near query: type

router.post("/", asyncHandler(async (req, res, next) => {
    const {lat, long} = req.body;
    const errors = [];
    if(typeof(lat) !== "number" || Math.abs(lat) > 90) errors.push("Lat is invalid");
    if(typeof(long) !== "number" || Math.abs(long) > 180) errors.push("Long is invalid");
    if(errors.length) return next(errors);
    const feed = await req.app.locals.db.collection("posts").aggregate(
        [
            {$geoNear:{
                near:{
                    type: "Point",
                    coordinates: [lat, long]
                },
                spherical:true,
                distanceField:"distance"
            }},
        ]
    );
    const feedArr = await feed.toArray();
    res.json(feedArr);
}));
4

1 回答 1

0

请参阅GeoJSON 对象

如果指定经纬度坐标,先列出经度,再列出纬度

你做了coordinates: [lat, long]

于 2021-03-13T08:45:01.190 回答