How to create table from node js in mongoose?

Here, we will write a program to create a table in mongoose with node js, considering that the connection has already been made and there is a database in place.

app.js file

// Import Mongoose
const mongoose = require('mongoose');

// Define the schema
const userSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  email: {
    type: String,
    required: true,
    unique: true
  },
  password: {
    type: String,
    required: true
  },
  createdAt: {
    type: Date,
    default: Date.now
  }
});

// Create the model
const User = mongoose.model('User', userSchema);

// Export the model
module.exports = User;

// Import the User model
const User = require('./models/user');

// Create a new user
const newUser = new User({
  name: 'rahul',
  email: 'abc@gmail.com',
  password: 'password_123'
});

// Save the user to the database
newUser.save()
  .then(() => console.log('User created'))
  .catch(err => console.log(err));