Shashika Virajh
Shashika Virajh

Reputation: 9477

Mongoose model seperations

Im new to node and mongodb. I have the following mongoose model.

import { model, Schema } from 'mongoose';
import Joi from '@hapi/joi';

const profileSchema = new Schema({
  user: {
    type: Schema.Types.ObjectId,
    ref: 'users',
  },
  handle: {
    type: String,
    minlength: 2,
    maxlength: 20,
    required: true,
    trim: true,
  },
  company: {
    type: String,
    minlength: 1,
    maxlength: 100,
    trim: true,
  },
  website: {
    type: String,
    maxlength: 100,
    trim: true,
  },
  location: {
    type: String,
    maxlength: 100,
    trim: true,
  },
  status: {
    type: String,
    maxlength: 50,
    trim: true,
    required: true,
  },
  skills: {
    type: [String],
    required: true,
  },
  bio: {
    type: String,
    maxlength: 500,
    trim: true,
  },
  githubUserName: {
    type: String,
    maxlength: 50,
    trim: true,
  },
  experience: [
    {
      title: {
        type: String,
        maxlength: 100,
        trim: true,
        required: true,
      },
      company: {
        type: String,
        maxlength: 100,
        trim: true,
        required: true,
      },
      location: {
        type: String,
        maxlength: 100,
        trim: true,
        required: true,
      },
      from: {
        type: Date,
        required: true,
      },
      to: {
        type: Date,
      },
      current: {
        type: Boolean,
        default: false,
      },
      description: {
        type: String,
        maxlength: 500,
        trim: true,
      },
    },
  ],
  education: [
    {
      school: {
        type: String,
        maxlength: 100,
        trim: true,
        required: true,
      },
      degree: {
        type: String,
        maxlength: 100,
        trim: true,
        required: true,
      },
      fieldOfStudy: {
        type: String,
        maxlength: 100,
        trim: true,
        required: true,
      },
      from: {
        type: Date,
        required: true,
      },
      to: {
        type: Date,
      },
      current: {
        type: Boolean,
        default: false,
      },
      description: {
        type: String,
        maxlength: 500,
        trim: true,
      },
    },
  ],
  social: {
    youtube: {
      type: String,
      maxlength: 100,
      trim: true,
    },
    twitter: {
      type: String,
      maxlength: 100,
      trim: true,
    },
    facebook: {
      type: String,
      maxlength: 100,
      trim: true,
    },
    linkedin: {
      type: String,
      maxlength: 100,
      trim: true,
    },
    instagram: {
      type: String,
      maxlength: 100,
      trim: true,
    },
  },
  date: {
    type: Date,
    default: Date.now,
  },
});

export default model('profile', profileSchema);

I have created this model in a single file and it seems too big. So should I put experience, education and social into 3 seperate models? If so how should I do it? If I put these in to 3 seperate models, how can I link them with the profile model? An example would be highly appriciated.

Upvotes: 0

Views: 32

Answers (1)

twcardenas
twcardenas

Reputation: 135

Yes, you should seperate them. To link them you would just put the profile schema Id as a field on the other models.

const profileSchema = new Schema({
  userId: Schema.Types.ObjectId
})

const experienceSchema = new Schema({
  userId: Schema.Types.ObjectId
})
const educationSchema = new Schema({
  userId: Schema.Types.ObjectId
})

Then you would just query the experience collection by the userId to get their experiences. This is the way I'd recommend.

Another way wouldbe to put experienceIds on the profile schema that would reference the Experience model and could use the populate method to fill the fields.

Upvotes: 1

Related Questions