mongodb - 如何创建一个模式以在 mongoDB 上创建一个地方使用 moongose
问题描述
我需要创建一个模式来添加餐厅,然后在地图上显示这个地方的反应。喜欢
1-名称地点
2-作者 3-纬度 4-日志 5-描述 6-开放时间
现在需要 id 用户连接如何在反应应用程序和反应本机应用程序上创建和显示。
解决方案
您可以使用 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"});
推荐阅读
- python - GEE Python API:导出表失败并出现意外错误
- c# - Datagrid组合框消失的内容+自动完成
- r - 里程半径上的限制标记
- tensorflow - 训练图像大小 Faster-RCNN
- javascript - 如何在 React 组件中使用 pickr 颜色选择器 vanilla JS 库?
- javascript - 使用 Cheerio 抓取多个网页
- javascript - 我应该如何处理两个函数“ui.resizable”和“css.rotate”的问题?
- mysql - mysql cte . 使用 with 和 insert it 会提示语法错误
- c - C字符串取消引用然后重新引用行为奇怪?
- django - 如何过滤多对多模型?