Skip to content

Instantly share code, notes, and snippets.

@heytulsiprasad
Last active July 26, 2020 08:18
Show Gist options
  • Select an option

  • Save heytulsiprasad/2986f7ee40b4a90203fcf7d1faf568af to your computer and use it in GitHub Desktop.

Select an option

Save heytulsiprasad/2986f7ee40b4a90203fcf7d1faf568af to your computer and use it in GitHub Desktop.
A comprehensive user profile mongoose model
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
// Create Schema
const PostSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: "users",
},
text: {
type: String,
required: true,
},
name: {
type: String,
},
avatar: {
type: String,
},
likes: [
{
user: {
type: Schema.Types.ObjectId,
ref: "users",
},
},
],
comments: [
{
user: {
type: Schema.Types.ObjectId,
ref: "users",
},
text: {
type: String,
required: true,
},
name: {
type: String,
},
avatar: {
type: String,
},
date: {
type: Date,
default: Date.now,
},
},
],
date: {
type: Date,
default: Date.now,
},
});
module.exports = Post = mongoose.model("post", PostSchema);
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
// Create Schema
const ProfileSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: "users",
},
handle: {
type: String,
required: true,
max: 40,
},
company: {
type: String,
},
website: {
type: String,
},
location: {
type: String,
},
status: {
type: String,
required: true,
},
// Array
skills: {
type: [String],
required: true,
},
bio: {
type: String,
},
githubusername: {
type: String,
},
// Array of objects
experience: [
{
title: {
type: String,
required: true,
},
company: {
type: String,
required: true,
},
location: {
type: String,
},
from: {
type: Date,
required: true,
},
to: {
type: Date,
},
current: {
type: Boolean,
default: false,
},
description: {
type: String,
},
},
],
// Array of objects
education: [
{
school: {
type: String,
required: true,
},
degree: {
type: String,
required: true,
},
fieldofstudy: {
type: String,
required: true,
},
from: {
type: Date,
required: true,
},
to: {
type: Date,
},
current: {
type: Boolean,
default: false,
},
description: {
type: String,
},
},
],
social: {
youtube: {
type: String,
},
twitter: {
type: String,
},
facebook: {
type: String,
},
linkedin: {
type: String,
},
instagram: {
type: String,
},
},
date: {
type: Date,
default: Date.now,
},
});
const Profile = mongoose.model("profile", ProfileSchema);
module.exports = Profile;
@heytulsiprasad
Copy link
Copy Markdown
Author

Self Notes

  • Every object that we create in models, has a mongo id of their own.
  • We can reference to any other model by simply writing.
profile: {
    type: Schema.Types.ObjectId,
    ref: "profiles"
}
  • We can also have an array of reference id's (like in a many to one relationship) for doing joins via referencing in Mongo db.
	likes: [
		{
			user: {
				type: Schema.Types.ObjectId,
				ref: "users",
			},
		},
	],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment