Easy Guide to Use findOneAndUpdate in MongoDB Using Nodejs

Using FindOneAndUpdate In MongoDB Featured Image

This blog will discuss how to use findOneAndUpdate in MongoDB using Nodejs.

When creating an application, you might want to allow users to make changes on a few things using the frontend that live inside your MongoDB database. Or, you might also want to search for some data inside our database and make changes for a variety of reasons.

To help you achieve this, you can use findOneAndUpdate in MongoDB using Nodejs. This guide will show you ways how you can use this method with Nodejs.

So let us get started.

How to Use findOneAndUpdate in MongoDB in Nodejs Application?

This guide assumes you already have MongoDB installed on your machine. If not, click here to see the steps to install MongoDB on Windows.

The guide will let us print the output on a webpage. Let us assume we are running a drone selling platform where users can sell their drones. We will allow them to edit their listings or even delete them.

Since it is a basic application, we will not focus on the authorization of the listings. Our project will also not have any styling, sorry 😊.

Follow the steps below to learn how.

  • Create a Nodejs application by navigating into the desired location. Open the terminal and pass this command:
npm init -y
  • Create the entry point file:
touch index.js
  • Install all the required packages:
npm i mongoose express method-override ejs

We will also install method-override API by Express to be able to use PUT or DELETE requests.

  • Now, open your code editor and use all the installed packages with the help of the require module:
const express = require('express');
const app = express();
const path = require('path');
const methodOverride = require('method-override');
const mongoose = require('mongoose');
const { urlencoded } = require('express');

We must also use the path module to help us manage the file paths. This will also help us run our Nodejs project from any directory.

  • Create a port where the app must listen:
app.listen(3000, () => {
    console.log('Connected to PORT 3000...');
  • Let us now add some of these lines of code below. We will be setting a MongoDB local database server connection using mongoose. Then, we are setting up the EJS view engine and assigning a folder to store our views.

We are also setting up the method override middleware in our application.

mongoose.connect('mongodb://localhost:27017/droneStore', { useNewUrlParser: true, useUnifiedTopology: true })
    .then(() => {
        console.log(`CONNECTED TO MONGO!`);
    .catch((err) => {
        console.log(`OH NO! MONGO CONNECTION ERROR!`);

app.set('views', path.join(__dirname, '/views'))
app.set('view engine', 'ejs')

app.use(express.urlencoded({ extended: true }))
  • Now, let us create our schema and model for the drones. Create a models folder in your project directory and inside it, create product.js. Here’s where our schema will reside and from where we will export our model:
const mongoose = require('mongoose');
const productSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true
    price: {
        type: Number,
        required: true,
        min: 0
    utility: {
        type: [String],
        enum: ['Monitoring or Inspection', 'Security', 'Delivery', 'Photography', 'Recreation']
    weight: {
        type: String,
        lowercase: true
    onSale: {
        type: Boolean,
        default: false
const Drone = mongoose.model('Drone', productSchema);
module.exports = Drone;
  • You will now have to import the exported model in index.js:
const Drone = require('./models/product.js')
  • I have already created some documents in my database before I proceed to use findOneAndUpdate in MongoDB. Now, I will create a basic GET route to the Edit page for a drones listing:
app.get('/drones/:id/edit', async (req, res) => {
    const { id } = req.params;
    const foundDrone = await Drone.findById(id);
    res.render('drones/edit.ejs', { foundDrone, utilities })

We can simply hit this route on our browser to visit the page.

  • To be able to edit the “utility” feature of a drone in the next step, I will create an array before the edit GET route consisting of the options. I will then iterate over this array in my view to be able to change the utility option:
const utilities = ['Monitoring or Inspection', 'Security', 'Delivery', 'Photography', 'Recreation'];
  • I will create a views folder in my project directory and create an edit.ejs file in it. This is the HTML markup it will consist of:

    Edit Drone


Edit <%= foundDrone.name %> Drone

Notice we have used the method override API to convert a POST request to PUT in Express. An HTML form only allows us to use two types of requests: GET or POST.

So, this is what our edit page looks like. You can simply copy and paste the ID of a document in the URL.

Use FindOneAndUpdate In MongoDB Edit Page
  • Let us finally create a PUT route to use findOneAndUpdate in MongoDB database and update the values:
app.put('/drones/:id', async (req, res) => {
    const { id } = req.params;
    const updateDrone = await Drone.findOneAndUpdate(id, req.body, { runValidators: true });
  • Let us edit some values on the SV – LaserX AW205 Drone. Once the document is updated it will redirect us to the view page of the document with the changes made.
Use FindOneAndUpdate In MongoDB Result Page


Learn to use findOneAndUpdate in MongoDB using Nodejs with this guide.