javascript - 在为地理空间查询定义索引后显示“IndexNotFound”错误
问题描述
我定义的索引:
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 latitude 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
}
},
{
$project: {
distance: 1,
name: 1
}
}
]);
res.status(200).json({
status: 'Success',
data: {
data: distances
}
});
});
我使用的路线是:
const router = express.Router();
router.route('/distances/:latlng/unit/:unit').get(tourController.getDistances);
我得到的错误:
{“状态”:“错误”,“错误”:{“操作时间”:“6963883450025639937”,“确定”:0,“代码”:27,“代码名称”:“IndexNotFound”,“$clusterTime”:{“clusterTime” ": "6963883450025639937", "签名": { "hash": "IhmwbnoiKdpWvx5rhQv2nnzmYiM=", "keyId": "6905064895109136388" } }, "name": "MongoError", "statusCode": 500, "status": "error " },
"message": "$geoNear 需要 2d 或 2dsphere 索引,但没有找到",
解决方案
推荐阅读
- python - 从 torchvision 预训练模型中获取模型类标签
- angular - const {something} = Plugins 是什么意思?
- php - Laravel Query Builder Join vs With Function,哪个更好?
- python - 在不使用 insert() 的情况下将项目插入 Python 列表
- javascript - 遍历订单记录并将重复值连接成字符串
- javascript - 从回调中做出承诺
- javascript - 如何将 Web MIDI API 与 React 前端和 Django 后端集成
- kubernetes - 创建 Kubernetes 部署时出错
- swift - Google Classroom API 与 swift 的集成
- python - Findall() 没有找到文本文件中的所有数字