-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud_mongo.js
82 lines (67 loc) · 1.53 KB
/
crud_mongo.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
71
72
73
74
75
76
77
78
79
80
81
82
const dbConnection = require('./mongodb')
const insert_one = async ()=>{
let data = await dbConnection('products');
data = await data.insert(
{
name:'note 5',
brand:'vivo',
price:2500
}
)
if (data.acknowledged){
console.log('data inserted')
}
}
// insert_one();
const insert_many = async ()=>{
let data = await dbConnection('products');
data = await data.insertMany(
[{
name:'c10',
brand:'Micromax',
price:1000
},
{
name:'c20',
brand:'OnePlus',
price:3000
}]
)
if (data.acknowledged){
console.log('Multiple rows added')
}
}
// insert_many();
const update = async ()=>{
let data = await dbConnection('products');
data = await data.updateOne(
{name:'m 40'},{$set:{name:'mi-40'}}
)
if (data.acknowledged){
console.log('Updated')
}
}
// update();
const update_many = async ()=>{
let data = await dbConnection('products');
data = await data.updateMany(
{brand:'vivo'},{$set:{brand:'oppo'}}
)
if (data.acknowledged){
console.log('updated')
}
}
// update_many();
const delete_data = async ()=>{
let data = await dbConnection('products')
data = await data.deleteMany(
{brand:'oppo'}
)
if (data.deletedCount>0){
console.log('data deleted')
}
else{
console.log('already deleted')
}
}
delete_data();