Node JS Crud Operation in Mongoose
Node JS Crud Operation in Mongoose
Node JS
Crud
Operations in
Mongoose
1 109 1st Floor, 208 2nd Floor, Prem Plaza, Ashok Nagar, Bhawarkua, Indore(M.P.)
Mob - 82368 09542, 75662 99542 Copyright © Arvind Choudhary
HCL (Higher Coding Language)
GET API in Mongoose
This is the connection file.
mongoose.js
const mongoose=require("mongoose")
mongoose.connect("mongodb://0.0.0.0:27017/HCL")
studentschema.js
const mongoose=require("mongoose")
const studentSchema=new mongoose.Schema({
rollno:Number,
name:String,
marks:Number
})
module.exports=mongoose.model("students",studentSchema)
index.js
const express=require("express")
const app=express()
require("./mongoose")
const student=require("./studentschema")
app.use(express.json())
app.get("/",async (req,resp)=>
{
const data=await student.find()
resp.send(data)
})
app.listen(5000)
Output
2 109 1st Floor, 208 2nd Floor, Prem Plaza, Ashok Nagar, Bhawarkua, Indore(M.P.)
Mob - 82368 09542, 75662 99542 Copyright © Arvind Choudhary
HCL (Higher Coding Language)
const express=require("express")
const app=express()
require("./mongoose")
const student=require("./studentschema")
app.use(express.json())
app.get("/",async (req,resp)=>
{
const data=await new
student({rollno:102,name:"Syam",marks:56})
const result=await data.save()
resp.send(result)
})
app.listen(5000)
Output
Output
3 109 1st Floor, 208 2nd Floor, Prem Plaza, Ashok Nagar, Bhawarkua, Indore(M.P.)
Mob - 82368 09542, 75662 99542 Copyright © Arvind Choudhary
HCL (Higher Coding Language)
In above program we are inserting data manually now will use postman to insert
data.
index.js
const express=require("express")
const app=express()
require("./mongoose")
const student=require("./studentschema")
app.use(express.json())
app.post("/",async (req,resp)=>
{
const data=await new student(req.body)
const result=await data.save()
resp.send(result)
})
app.listen(5000)
Input
4 109 1st Floor, 208 2nd Floor, Prem Plaza, Ashok Nagar, Bhawarkua, Indore(M.P.)
Mob - 82368 09542, 75662 99542 Copyright © Arvind Choudhary
HCL (Higher Coding Language)
Output
const express=require("express")
const app=express()
require("./mongoose")
const student=require("./studentschema")
app.use(express.json())
app.get("/",async (req,resp)=>
{
const data=await
student.updateOne({rollno:101},{$set:{name:"Golu"}})
resp.send(data)
})
5 109 1st Floor, 208 2nd Floor, Prem Plaza, Ashok Nagar, Bhawarkua, Indore(M.P.)
Mob - 82368 09542, 75662 99542 Copyright © Arvind Choudhary
HCL (Higher Coding Language)
app.listen(5000)
Output
Output
In above program we are Updating data manually now will use postman to insert
data.
index.js
const express=require("express")
const app=express()
require("./mongoose")
const student=require("./studentschema")
app.use(express.json())
app.put("/",async (req,resp)=>
{
const data=await
student.updateOne({rollno:req.body.rollno},{$set:req.body})
resp.send(data)
})
app.listen(5000)
6 109 1st Floor, 208 2nd Floor, Prem Plaza, Ashok Nagar, Bhawarkua, Indore(M.P.)
Mob - 82368 09542, 75662 99542 Copyright © Arvind Choudhary
HCL (Higher Coding Language)
Input
Output
const express=require("express")
const app=express()
require("./mongoose")
const student=require("./studentschema")
app.use(express.json())
app.get("/",async (req,resp)=>
{
const data=await student.deleteOne({rollno:104})
resp.send(data)
7 109 1st Floor, 208 2nd Floor, Prem Plaza, Ashok Nagar, Bhawarkua, Indore(M.P.)
Mob - 82368 09542, 75662 99542 Copyright © Arvind Choudhary
HCL (Higher Coding Language)
})
app.listen(5000)
Output
In above program we are Deleting data manually now will use postman to insert
data.
index.js
const express=require("express")
const app=express()
require("./mongoose")
const student=require("./studentschema")
app.use(express.json())
app.delete("/",async (req,resp)=>
{
const data=await
student.deleteOne({rollno:req.body.rollno})
resp.send(data)
})
app.listen(5000)
index.js
const express=require("express")
const app=express()
require("./mongoose")
const student=require("./studentschema")
app.use(express.json())
8 109 1st Floor, 208 2nd Floor, Prem Plaza, Ashok Nagar, Bhawarkua, Indore(M.P.)
Mob - 82368 09542, 75662 99542 Copyright © Arvind Choudhary
HCL (Higher Coding Language)
app.delete("/:rollno",async (req,resp)=>
{
const data=await student.deleteOne(req.params)
resp.send(data)
})
app.listen(5000)
Input
Search API
In search API we can search data with respect to any single field or multiple
fields.
mongoose.js
const mongoose=require("mongoose")
9 109 1st Floor, 208 2nd Floor, Prem Plaza, Ashok Nagar, Bhawarkua, Indore(M.P.)
Mob - 82368 09542, 75662 99542 Copyright © Arvind Choudhary
HCL (Higher Coding Language)
mongoose.connect("mongodb://0.0.0.0:27017/HCL")
studentschema.js
const mongoose=require("mongoose")
const studentSchema=new mongoose.Schema({
rollno:Number,
name:String,
marks:Number
})
module.exports=mongoose.model("students",studentSchema)
index.js
const express=require("express")
const app=express()
require("./mongoose")
const student=require("./studentschema")
app.use(express.json())
app.get("/:key",async (req,resp)=>
{
console.log(req.params.key)
const data=await student.find(
{
"$or":[
{ "name":{$regex:req.params.key}}
]
})
resp.send(data)
})
app.listen(5000)
10 109 1st Floor, 208 2nd Floor, Prem Plaza, Ashok Nagar, Bhawarkua, Indore(M.P.)
Mob - 82368 09542, 75662 99542 Copyright © Arvind Choudhary
HCL (Higher Coding Language)
Output
index.js
const express=require("express")
const app=express()
require("./mongoose")
const student=require("./studentschema")
app.use(express.json())
app.get("/:key",async (req,resp)=>
{
console.log(req.params.key)
const data=await student.find(
{
"$or":[
{"name":{$regex:req.params.key}},
{"rollno":{$regex:req.params.key}},
{"marks":{$regex:req.params.key}}
]
})
resp.send(data)
})
app.listen(5000)
11 109 1st Floor, 208 2nd Floor, Prem Plaza, Ashok Nagar, Bhawarkua, Indore(M.P.)
Mob - 82368 09542, 75662 99542 Copyright © Arvind Choudhary
HCL (Higher Coding Language)
12 109 1st Floor, 208 2nd Floor, Prem Plaza, Ashok Nagar, Bhawarkua, Indore(M.P.)
Mob - 82368 09542, 75662 99542 Copyright © Arvind Choudhary
HCL (Higher Coding Language)
ML Algortithms
19 Python With AI Core Python, Advanced 6 months
Python,Numpy,Pandas, Mitlab, Seaborn &
AI Algortithms
20 Python Data science Core Python, Advanced 6 months
Python,Numpy,Pandas, Mitlab, Seaborn &
Data Science Algortithms
20 Python Data Analytics Core Python, Advanced 6 months
Python,Numpy,Pandas, Mitlab, Seaborn
,mysql and power BI
13 109 1st Floor, 208 2nd Floor, Prem Plaza, Ashok Nagar, Bhawarkua, Indore(M.P.)
Mob - 82368 09542, 75662 99542 Copyright © Arvind Choudhary
HCL (Higher Coding Language)
Rohan Sisodiya GeeCom India 1.2 LPA
14 109 1st Floor, 208 2nd Floor, Prem Plaza, Ashok Nagar, Bhawarkua, Indore(M.P.)
Mob - 82368 09542, 75662 99542 Copyright © Arvind Choudhary
HCL (Higher Coding Language)
Dheeraj Patel Cognizant 4.50
15 109 1st Floor, 208 2nd Floor, Prem Plaza, Ashok Nagar, Bhawarkua, Indore(M.P.)
Mob - 82368 09542, 75662 99542 Copyright © Arvind Choudhary
HCL (Higher Coding Language)
Ajay Sailani Avery Bit 1.20 LPA
Other Facility
16 109 1st Floor, 208 2nd Floor, Prem Plaza, Ashok Nagar, Bhawarkua, Indore(M.P.)
Mob - 82368 09542, 75662 99542 Copyright © Arvind Choudhary