$ geoNear требует 2d или 2dsphere индекса, но ни один не был найден - PullRequest
0 голосов
/ 02 мая 2020

Добавьте schema.index ({startlocation: '2dsphere'}) в схему, но не можете удалить ошибку. схема tourSchema.index ({startLocation: '2dsphere'}); -> эта строка добавлена ​​в модель

controller

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,
 },
 });
 });

"error": "message": "$ geoNear требуется индекс 2d или 2dsphere, но ничего не найдено",

Я получаю эту ошибку, пожалуйста, помогите мне с этим. Заранее спасибо

...