DEV Community

Himanshupal0001
Himanshupal0001

Posted on

2 1

#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)

Cloudinary image

Video API: manage, encode, and optimize for any device, channel or network condition. Deliver branded video experiences in minutes and get deep engagement insights.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay