In which Mongoose Schema I should store the reference “_id”

岁酱吖の 提交于 2020-07-09 11:49:26

问题


I have designed my schemas in this manner.

User Schema

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
});

Location Schema

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 }
)};

Review Schema

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 }
});

Explanation

A User can add multiple Post of a single Location. A User can add single Review of a single Location.

Question

Q1 Is this the correct way to store references "_id" for the relationships. Q2 Are these Schema valid? if not please Suggest me the Schemas the way they should be.

来源:https://stackoverflow.com/questions/61559376/in-which-mongoose-schema-i-should-store-the-reference-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!