首页 > 解决方案 > 如何创建一个模式以在 mongoDB 上创建一个地方使用 moongose

问题描述

我需要创建一个模式来添加餐厅,然后在地图上显示这个地方的反应。喜欢

1-名称地点
2-作者 3-纬度 4-日志 5-描述 6-开放时间

现在需要 id 用户连接如何在反应应用程序和反应本机应用程序上创建和显示。

标签: mongodbexpressmongoose

解决方案


您可以使用 2dsphere 索引来保存餐厅的位置,它可以帮助您找到最近的餐厅或计算距离。

   const mongoose = require("mongoose");
const { Schema } = mongoose;

const restaurtSchema = new Schema({
  title: { type: String, required: true, trim : true },
  description: { type: String, required: true },
  image: { type: String },
  address: { type: String, required: true },
  locationLongLat: {
        'type': {type: String, enum: "Point", default: "Point"},
        coordinates: {type: [Number], default: [0, 0]}
    }
  creator: { type: mongoose.Types.ObjectId, required: true, ref: 'User'},
  date: { type: Date, default: Date.now },
});
restaurtSchema.index({'locationLongLat.coordinates': "2dsphere"});  

推荐阅读