title: Deleting all records when trying to delete single row in Node express mongoDb
body: <p>I am showing data of several users in my application where there is a functionality of edit and delete of individual record.I am using Node express framework and using MongoDb as a database.My problem is when I am deleting any specific record then all the records are deleting from the table and when I am showing the result in console it is showing only that record on which I intended to delete.</p>
  <p>Below is my code:</p>
  <p><strong>index.ejs</strong></p>
  <pre><code>div class=&quot;container&quot;&gt;
  
   &lt;% if(users.length&gt;0) {%&gt;
  
      &lt;p&gt;Total available users - &lt;%= users.length  %&gt; &lt;/p&gt;
  
     
        &lt;table class=&quot;table&quot;&gt;
  
          &lt;thead class=&quot;thead-dark&quot;&gt;
            &lt;tr&gt;
              &lt;th scope=&quot;col&quot;&gt;Name&lt;/th&gt;
              &lt;th scope=&quot;col&quot;&gt;Email&lt;/th&gt;
              &lt;th scope=&quot;col&quot;&gt;City&lt;/th&gt;
              &lt;th scope=&quot;col&quot;&gt;Action&lt;/th&gt;
            &lt;/tr&gt;
          &lt;/thead&gt;
  
  
          &lt;tbody&gt;
           &lt;%  for(var i=0;i&lt;users.length;i++) { %&gt;
             &lt;tr&gt;
              &lt;td&gt;&lt;%= users[i].name %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= users[i].email %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= users[i].city %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;a href=&quot;/edit/&lt;%= users[i]._id %&gt;&quot;&gt;Edit&lt;/a&gt; | &lt;a id = &quot;del&quot; href=&quot;/delete/&lt;%= users[i]._id %&gt;&quot;&gt; Delete &lt;/a&gt; &lt;/td&gt;
             &lt;/tr&gt;
           &lt;% }  %&gt;  
          &lt;/tbody&gt;
        
        &lt;/table&gt;
  
    
    &lt;% } else{ %&gt;
  
       &lt;p&gt;No data found&lt;/p&gt;
   &lt;%  }  %&gt;  
  
  &lt;/div&gt;
  </code></pre>
  <p>This yields below ui:</p>
  <p><a href="https://i.stack.imgur.com/6SbeF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6SbeF.png" alt="enter image description here" /></a></p>
  <p><strong>Student.js Schema</strong></p>
  <pre><code>const mongoose = require('mongoose');
  
  const studentSchema = new mongoose.Schema({
  
    name:{
      type:String,
      required:true
    },
    email:{
      type:String,
      required:true
    },
    city:{
      type:String,
      required:true
    },
    timeCreated:{
      type:Date,
      default:()=&gt;Date.now()
    }
  });
  
  module.exports = mongoose.model('Student',studentSchema);
  </code></pre>
  <p><strong>server.js</strong></p>
  <pre><code>const express = require('express');
  const path = require('path');
  const bodyParser = require('body-parser');
  const mongoose = require('mongoose');
  const Student = require('./models/Student');
  const dotEnv = require('dotenv').config();
  const cors =  require('cors');
  
  const app = express();
  
  const port = process.env.PORT || 3000;
  
  app.use(cors());
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({extended:true}));
  
  
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine','ejs');
  
  app.use(&quot;/public&quot;, express.static('public')); 
  app.use(express.static(path.join(__dirname,'public')));
  
  app.get('/delete/:id',(req,res) =&gt; {
  
  mongoose.connect(process.env.URI,{useNewUrlParser:true,
          useUnifiedTopology:true}).then(() =&gt;
  
              Student.findOneAndDelete(req.params.id, (err,result) =&gt; {
  
                  if(err){
                      console.log(&quot;Error&quot;,err);
                  }
                  else{
                      res.redirect('/');
                      console.log(result + &quot; - &quot; + req.params.id);
                  }
              })
           
           ).catch((err) =&gt; console.log(err));
  
  });
  
  app.listen(port,() =&gt; console.log(`App is running at ${port}`));
  </code></pre>
  <p>Someone let me know why all the records are deleting instead of one which I want to delete.</p>