$ Geonear在聚合管道中不起作用

时间:2020-06-06 07:03:45

标签: node.js mongodb mongoose

我有一个旅游收藏,其中包含以下模型

const tourSchema = new mongoose.Schema(
  {
    slug: {
      type: String,
    },
    name: {
      type: String,
      required: ['true', 'a tour must have a name'],
      unique: true,
      trim: true,
      maxlength: [40, 'a tour name must not be more than 40 characters'],
      minlength: [10, 'a tour must not be less than 10 characters'],
      // validate: [validator.isAlpha, 'name should only contain A-Z'],
    },
    duration: {
      type: Number,
      required: [true, 'a tour must have a duration'],
    },
    maxGroupSize: {
      type: Number,
      required: [true, 'a tour must have a group size'],
    },
    difficulty: {
      type: String,
      required: [true, 'a tour should have a difficulty'],
      enum: {
        values: ['easy', 'medium', 'difficult'],
        message: 'Difficulty is either easy, medium or difficult',
      },
    },
    ratingsAverage: {
      type: Number,
      default: 4.5,
      min: [1, 'Rating must be more than 1'],
      max: [5, 'Maximum rating is 5'],
      set: (val) => Math.round(val * 10) / 10,
    },
    ratingsQuantity: {
      type: Number,
      default: 0,
    },
    price: {
      type: Number,
      required: ['true', 'a tour must have a price'],
    },
    priceDiscount: {
      type: Number,
      validate: {
        validator: function (val) {
          // this only points to current doc on NEW document creation
          return val < this.price;
        },
        message: 'Discount price {VALUE} should be lower than regualr price',
      },
    },
    summary: {
      type: String,
      trim: true,
      required: ['true', 'a tour must have a description'],
    },
    description: {
      type: String,
      trim: true,
    },
    imageCover: {
      type: String,
      required: ['true, a tour must have a cover image'],
    },
    images: [String],
    createdAt: {
      type: Date,
      default: Date.now(),
      select: false,
    },
    startDates: [Date],
    secretTour: {
      type: Boolean,
      default: false,
    },
    startLocation: {
      // GEOJSON
      type: {
        type: String,
        default: 'Point',
        enum: ['Point'],
      },
      coordinates: [Number],
      address: String,
      description: String,
    },
    locations: [
      {
        type: {
          type: String,
          default: 'Point',
          enum: ['Point'],
        },
        coordinates: [Number],
        address: String,
        description: String,
        day: Number,
      },
    ],
    guides: [{ type: mongoose.Schema.ObjectId, ref: 'User' }],
  },
  {
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  }
);

我写了这样的类型 $ GeoNear 的集合:

 const distances = await Tour.aggregate([
    {
      $geoNear: {
        near: {
          type: 'Point',
          coordinates: [lng * 1, lat * 1],
        },
        distanceField: 'distance',
        distanceMultiplier: multiplier,
      },
    },
  ]);

我已经阅读了官方文档,以及我在网上可以找到的所有其他内容,毫不动摇

我知道我应该有一个'2dSphere'索引。它存在于我的mongoDB上。我还确保这是管道中的第一个聚合

但是结果总是空的!这真令人沮丧!可能是因为这个https://github.com/parse-community/parse-server/pull/6540。任何意见都非常感谢。

0 个答案:

没有答案