DEV Community

Himanshupal0001
Himanshupal0001

Posted on

#4. Creating models for mongoDb collections ☀

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

Top comments (0)