我正在使用mongoDB和Node.js驱动程序为MongoDB开发一个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}
和{lat:47.643628614308675, long:-122.34560056016635}
的错误。我能找到的唯一模式是它讨厌西海岸的坐标。我已经证实2d-球形指数仍然存在于数据中。我试过把球设为假的。我甚至尝试删除所有的数据,即使是在一个空的集合中,我也会得到相同的行为。我真的很困惑,为什么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);
}));
发布于 2021-03-13 08:45:01
https://stackoverflow.com/questions/66611269
复制相似问题