How to update data in table from node js in mongoose.

Here, we will write a program to update the data into a table in mongoose using node js. First step is to create a database and a table and insert some data into it which has already been done in the previous posts.

app.js

// 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));

// Define the filter and update
const filter = { email: 'abc@gmail.com' };
const update = { name: 'rahul', password: 'newpassword_123' };

// Update the document in the database
User.updateOne(filter, update)
  .then(result => console.log(result))
  .catch(err => console.log(err));