Я разработал свои схемы таким образом.
Схема пользователя
const userSchema = mongoose.Schema({
first_name: {type: String, required: true},
last_name: {type: String},
email: {type: String, required: true, unique: true},
password: {type: String, required: true},
isVerified: {type: Boolean, required: false},
dob: {type: Date},
from: {
city: {type: String},
state: {type: String}
},
gender: {type: String},
phone_no: {type: Number},
cover_pic: {type: String},
profile_pic: {type: String},
about:{type: String},
social:{
instagram:{type: String},
facebook:{type: String},
youtube:{type: String},
twitter:{type: String}
}
},{
timestamps: true
});
Схема расположения
const locationSchema = mongoose.Schema({
name : { type: String, required: true },
address : {
street: { type: String, required: true },
area: { type: String, required: true },
city: { type: String, required: true },
zip_code: { type: Number, required: true },
state: { type: String, required: true }
},
description : { type: String, required: true },
lat : { type: Number, required: true },
long : { type: Number, required: true },
phone_number : { type: Number, required: true },
facilities : [{type: String}],
location_type : { type: String, required: true },
location_website : { type: String, required: true }
)};
Схема просмотра
const reviewSchema = mongoose.Schema({
rating: {type: Number, require: true},
review: {type: String, require: true},
user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' ,required: true },
location: { type: mongoose.Schema.Types.ObjectId, ref: 'Location' ,required: true }
});
reviewSchema.index({ user: 1, location: 1 }, { unique: true });
Post Schema
const postSchema = mongoose.Schema({
title: { type: String, required: true },
description: { type: String, required: true },
imagesPaths: [String],
videosPaths: [String],
creatorId: { type: mongoose.Schema.Types.ObjectId, ref: 'User' ,required: true },
location: { type: mongoose.Schema.Types.ObjectId, ref: 'Location' ,required: true }
});
Пояснение
A User
может добавить несколько Post
одного Location
. User
может добавить один Review
одного Location
.
Вопрос
Q1 Это правильный способ хранения ссылок "_id" для отношения. Q2 Действительны ли эти схемы? если нет, пожалуйста, предложите мне схемы, какими они должны быть.