【发布时间】:2020-05-02 14:22:42
【问题描述】:
在架构中添加 schema.index({startlocation: '2dsphere'}) 但无法清除错误。 图式 tourSchema.index({ startLocation: '2dsphere' }); --> 这一行是在模型中添加的
控制器
exports.getDistances = catchAsync(async (req, res, next) => {
const { latlng, unit } = req.params;
const [lat, lng] = latlng.split(',');
const multiplier = unit === 'mi' ? 0.000621371 : 0.001;
if (!lat || !lng) {
next(
new AppError(
'Please provide latitutr and longitude in the format lat,lng.',
400
)
);
}
const distances = await Tour.aggregate([
{
$geoNear: {
near: {
type: 'Point',
coordinates: [lng * 1, lat * 1],
},
distanceField: 'distance',
distanceMultiplier: multiplier,
spherical: true,
},
},
{
$project: {
distance: 1,
name: 1,
},
},
]);
res.status(200).json({
status: 'success',
data: {
data: distances,
},
});
});
“错误”: "message": "$geoNear 需要 2d 或 2dsphere 索引,但没有找到",
我收到此错误,请帮助我解决此问题。 提前致谢
【问题讨论】:
标签: node.js mongoose mongodb-indexes