猫鼬地理位置,获取属于不同猫鼬模型的位置所订购的产品

时间:2020-01-25 04:31:04

标签: mongodb mongoose mongoose-schema

我正在创建一个商店经理应用程序。在这里,用户应该能够使用产品查询来查询产品。 条件是那些产品应按照到产品商店的距离订购。

例如:如果用户正在搜索铅笔,则应退回15公里以内的所有属于商店的名为Pencil的产品,并应按到商店的距离对结果进行排序。

//产品型号

const productSchema = new mongoose.Schema({
  name: {
    type: String,
    trim: true,
    required: 'Product name is required'
  },
  store: {
    type: mongoose.Schema.ObjectId,
    ref: 'Store',
    required: 'Store must be connected to the user'
  }
});

//商店型号

const storeSchema = new mongoose.Schema({
  name: {
    type: String,
    trim: true,
    required: 'Please enter a store name'
  },
  location: {
    type: {
      type: String,
      default: 'Point'
    },
    coordinates: [{
      type: Number,
      required: 'You must supply coordinates!'
    }]
  },
});

这就是我查询产品的方式

  const products = await Product.find({
    name : args.name
    store:{
         near: { type: "Point", coordinates: [ 79.8612, 6.9271] },
         maxDistance: store.maxDistance,
         spherical: true
    }
  });

不幸的是,它给了我错误

CastError: Cast to ObjectId failed for value "{
  near: { type: 'Point', coordinates: [ 79.8612, 6.9271 ] },
  maxDistance: 15,
  spherical: true
}

我相信我已经正确创建了索引

storeSchema.index({
  location: '2dsphere'
});

0 个答案:

没有答案