-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRUD_Operation_with_mongoose.js
70 lines (51 loc) · 1.45 KB
/
CRUD_Operation_with_mongoose.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const express=require('express')
const app=express();
const mongoose=require('mongoose');
const databasename='e-commerce';
const collectionName='product';
mongoose.connect(`mongodb://localhost:27017/${databasename}`);
const productschema= new mongoose.Schema(
{
Name: String,
Price: Number,
Category: String,
}
)
const modelfunction= async ()=>{
const producModel= mongoose.model(`${collectionName}`,productschema)
let data= new producModel(
{
Name:"potato",
Price:50,
Category:"vegetable"
}
)
let result= await data.save();
}
// update data
const updateInDb= async ()=>{
const productModel=mongoose.model(`${collectionName}`,productschema)
const update= await productModel.updateMany(
{Name:"potato"},
{$set:{Price:600,Name:"onion"}}
)
}
//delete data
const deleteInDb= async ()=>{
const productModel= mongoose.model(`${collectionName}`,productschema)
const deletedata=await productModel.deleteMany({Name:'onion'});
console.log(deletedata);
}
//find data
const findInDb=async()=>{
const productModel= mongoose.model(`${collectionName}`,productschema)
const finddata=await productModel.find()
app.get('/',(req,res)=>{
res.send(finddata)
})
}
findInDb()
deleteInDb();
updateInDb();
modelfunction();
app.listen(3000);