Tutorial
Model 1 - User

User Schema

Creating a Mongoose Model in user.js File

server
├── models
│   └── user.js
user.js
 
import mongoose from "mongoose";
 
const UserSchema = new mongoose.Schema(
  {
    firstName: {
      type: String,
      required: true,
      min: 2,
      max: 50,
    },
    lastName: {
      type: String,
      required: true,
      min: 2,
      max: 50,
    },
    email: {
      type: String,
      required: true,
      max: 50,
      unique: true,
    },
    password: {
      type: String,
      required: true,
      min: 5,
    },
    picturePath: {
      type: String,
      default: "",
    },
    friends: {
      type: Array,
      default: [],
    },
    location: String,
    occupation: String,
    viewedProfile: Number,
    impressions: Number,
  },
  { timestamps: true }
);
 
const User = mongoose.model("User", UserSchema);
export default User;

When building a Node.js application that interacts with a MongoDB database, it is crucial to define the schema for the data you want to store. This is done with Mongoose, an object modeling tool that provides a simple and flexible way of defining the schema for your MongoDB collections. In this script, we create a new folder called models and inside it, we create a file called user.js to define the schema for a user model.

Importing Mongoose

We start by importing Mongoose using ES6 syntax:

import mongoose from "mongoose";

Defining the User Schema

The next step is to define the User schema using the mongoose.Schema constructor. We pass an object containing the fields we want to define for the user model as the first argument. For example:

const UserSchema = new mongoose.Schema({
  firstName: {
    type: String,
    required: true,
    min: 2,
    max: 50,
  },
  lastName: {
    type: String,
    required: true,
    min: 2,
    max: 50,
  },
  email: {
    type: String,
    required: true,
    max: 50,
    unique: true,
  },
  password: {
    type: String,
    required: true,
    min: 5,
  },
  picturePath: {
    type: String,
    default: "",
  },
  friends: {
    type: Array,
    default: [],
  },
  location: String,
  occupation: String,
  viewedProfile: Number,
  impressions: Number,
},
{ timestamps: true });
 

Here, we define the following fields for the user model:

  • firstName: A string that is required, has a minimum length of 2, and a maximum length of 50.
  • lastName: A string that is required, has a minimum length of 2, and a maximum length of 50.
  • email: A string that is required, has a maximum length of 50, and must be unique.
  • password: A string that is required and has a minimum length of 5.
  • picturePath: A string that is not required and has a default value of an empty string.
  • friends: An array that is not required and has a default value of an empty array.
  • location: A string that is not required.
  • occupation: A string that is not required.
  • viewedProfile: A number that is not required.
  • impressions: A number that is not required.

We also add a second argument to the mongoose.Schema constructor, an object containing the timestamps property set to true. This will automatically create createdAt and updatedAt fields for our user model.

Creating a Mongoose Model

After defining the user schema, we create a Mongoose model using the mongoose.model() method. We pass the model name as the first argument, which is "User" in this case, and the schema we defined earlier as the second argument.

const User = mongoose.model("User", UserSchema);

Exporting the User Model

Finally, we export the User model so that we can use it in other parts of our application.

export default User;

Conclusion

Creating a user model in Mongoose is an essential step when building a Node.js application that interacts with MongoDB. In this script, we create a user model schema and create a Mongoose model that can be used to query and manipulate data in a MongoDB database. Understanding how to define and create Mongoose models is crucial when building any Node.js application that uses MongoDB.