-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRUD-With-Mongoose.js
63 lines (54 loc) · 1.46 KB
/
CRUD-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
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost:27017/e-comm");
//Defining the schema of db
const productSchema = new mongoose.Schema({
name: String,
price: Number,
brand: String,
category: String
});
//defining/creating model for the db
const insertInDB = async ()=>{
const productModel = mongoose.model("products", productSchema);
let data = new productModel({
name: "m 10",
price: 1000,
brand: "maxx",
category: "mobile",
});
let result = await data.save();
console.log(result);
}
// Update in DB
const updateIndb = async()=>{
const Product = mongoose.model('products' , productSchema);
let data = await Product.updateOne(
{name:"m 10"}, //condition
{
$set:{price:750, name:"max 8"} //data to be updated
}
)
console.log(data);
}
//Delete the record
const deleteInDB = async ()=>{
const Product = mongoose.model('products' , productSchema);
let data = await Product.deleteOne({name:'max 8'});
console.log(data);
}
//Reading the records
const findInDB = async ()=>{
const Product = mongoose.model('products' , productSchema);
let data = await Product.find();
console.log(data);
}
//Reading any specific record
const findOneInDB = async ()=>{
const Product = mongoose.model('products' , productSchema);
let data = await Product.find({name:'m20'});
console.log(data);
}
updateIndb ();
deleteInDB();
findInDB();
findOneInDB();