Node JS

How to Search Records Node js Express Mongodb

In this articles will teach how how to search records Node js Express Mongodb.how the search functionality works.

Create the Node js Project using following command

npm init

Install the following dependencies

  • npm i mongoose
  • npm i express
  • npm install –save-dev nodemon

Server.js

const express = require('express')
const sever = express()
const port = 4000
var routes = require('./routes/routes');
var mongoose = require('mongoose');
mongoose.set('strictQuery', true);
mongoose.connect("mongodb://127.0.0.1:27017/dbschool",{useNewUrlParser: true,  useUnifiedTopology: true },function checkDb(error)
{
    if(error)
    {
        console.log(error);
    }
    else
    {
        console.log("successfully Connected to DB");
    }
});

sever.use(express.json());
sever.use(routes);
sever.listen(port, () => {
  console.log(`Express port successfully ${port}`)
})

routes.js

var express = require('express');
var userController = require('../src/employee/userController');

const router = express.Router();

router.route('/user/findOne/:name').get(userController.findOneUserController);

module.exports = router;

userController.js

var userService = require('./userService');

var findOneUserController = async (req, res) => 
{  
    console.log(req.params.name );
    var result = await userService.findOneUserDBService(req.params.name );

    if (result) {
        res.send({ "status": true, "data": result} );
    } else {
        res.send({ "status": false, "data": "User not found" });
    }
}

module.exports = { findOneUserController};

userModel.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var userSchema = new Schema({

    name: {
        type: String,
        required: true
    },
    address: {
        type: String,
        required: true
    },
    phone: {
        type: String,
        required: true
    }
   

});

module.exports = mongoose.model('employees', userSchema);

userService.js

var userModel = require('./userModel');

module.exports.findOneUserDBService = (userDetais) => {
    return new Promise(function myFn(resolve, reject) {
        userModel.findOne({name:userDetais}, function returnData(error, result) {
          if(error)
          {
                reject(false);
          }
          else
          {
             resolve(result);
          }
        });
 
    });
 
 }

i have attached the video link below. which will do this tutorials step by step.

 

admin

Recent Posts

Employee Working Hours Calculation System using C#.net

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

1 week ago

Java Payroll System Calculate Employee Overtime

Act as a Java developer to create a program that calculates the gross wage for…

1 week ago

Employee Working Hours Calculation System using Java

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

2 weeks ago

Laravel 11 School Management System

In this tutorial, we will teach you how to create a simple school management system…

2 weeks ago

How to Make Admin Panel Using React MUI

I have design the Admin Basic templete using React MUI Design Admin Dashboard and Login.Here…

4 weeks ago

Install Laravel Breeze for Authentication Using Laravel 11

In this tutorial ,i am to going teach the Laravel Breeze.Laravel Breeze provides a simple…

1 month ago