Aquasar
  • Home
  • Portfolio
  • Articles
  • Pricing
  • About
  • Contact
WEB DEVELOPMENT |SEO |DIGITAL ADS

Creating models in Mongoose

Aug 23rd, 2019

Alex Quasar

mongo

Models are constructors that take a schema (logic on how we store the data) and allow us to return a document ( an instance of a particular schema)

const mongoose = require('mongoose');

const UserSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  email: {
    type: String,
    required: true,
    unique: true
  },
  password: {
    type: String,
    required: true
  },
  avatar: {
    type: String
  },
  date: {
    type: Date,
    default: Date.now
  }
});

module.exports = User = mongoose.model('user', UserSchema);

The model method of mongoose, takes in two arguments. 1. The name of the model that we give it and the Schema that we defined above.