#4. Creating models for mongoDb collections ☀
Himanshupal0001

Himanshupal0001 @himanshupal0001

About: Full stack web Dev. Like to teach and learn. Open for free lance and open source projects.

Location:
DXB
Joined:
Nov 21, 2021

#4. Creating models for mongoDb collections ☀

Publish Date: Jul 13 '22
5 0

Today we'll see How to create model for mongoDB in js.

  • Create a folder name models in vscode directory.

  • Create a file 'Users.js' as we prefer capital latter for models.

Below is the ss of directory

Image description

const mongoose = require('mongoose');

const UserSchema = 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);
Enter fullscreen mode Exit fullscreen mode

We have used mongoose as a lib for mongoDb.

YT link for reference

Comments 0 total

    Add comment