Here, we will write a program to insert data into table in mongoose using node js, considering there is a table in the database and a connection has already been made.
app.js file
// Import Mongoose
const mongoose = require('mongoose');
// Import the User model
const User = require('./models/user');
// Connect to the database
mongoose.connect('mongodb://localhost/mydatabase', { useNewUrlParser: true })
.then(() => console.log('Connected to database'))
.catch(err => console.log(err));
// Create a new user
const newUser = {
name: 'rahul',
email: 'abc@gmail.com',
password: 'password_123'
};
// Insert the user into the database
User.create(newUser)
.then(() => console.log('User inserted'))
.catch(err => console.log(err));