-
Notifications
You must be signed in to change notification settings - Fork 0
/
extra.txt
152 lines (122 loc) · 3.24 KB
/
extra.txt
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//Insertion using an Instance
const student1 = new Student({name: 'Swapnil', roll: 1, gender: 'Male', std: 'XII'});
student1.save()
.then(data => {
console.log("One Student Data added successfully.");
console.log(data);
})
.catch(err => {
console.log("Error occured during insertion.");
console.log(err);
})
//Insertion using a Model method
Student.insertMany([
{
name: 'Chokshi',
roll: 2,
gender: 'Male'
},
{
name: 'Shyam',
roll: 3,
gender: 'Female'
},
{
name: 'Krishna',
roll: 4,
gender: 'Female',
std: 'X'
}])
.then(data => {
console.log("Multiple Student Data added successfully");
console.log(data);
})
.catch(err => {
console.log("Error occured during multiple insertions.");
console.log(err);
})
//Deletion
Student.deleteOne({name: 'Chokshi'})
.then(msg => {
console.log("One item deleted successfully.");
console.log(msg);
})
.catch(err => {
console.log("Error occured during deletion.");
console.log(err);
})
Student.deleteMany({gender: 'Male'})
.then(msg => {
console.log("Multiple items deleted successfully.");
console.log(msg);
})
.catch(err => {
console.log("Error occured during deletions.");
console.log(err);
})
//Updation
Student.updateOne({name: 'Shyam'}, {gender: 'Male'})
.then(msg => {
console.log("One item updated successfully.");
console.log(msg);
})
.catch(err => {
console.log("Error occured during updation.");
console.log(err);
})
Student.updateMany({name: {$in: ['Swapnil','Shyam']}}, {std: 'IX'})
.then(msg => {
console.log("Multiple items updated successfully.");
console.log(msg);
})
.catch(err => {
console.log("Error occured during updations.");
console.log(err);
})
//Finding
Student.find({roll: 3})
.then(data => console.log(data))
.catch(err => {
console.log("Data not found.");
console.log(err);
})
Student.findOne({std: 'XII'})
.then(data => console.log(data))
.catch(err => {
console.log("Data not found.");
console.log(err);
})
Student.findOneAndUpdate({name: 'Krishna'}, {std: 'V'}, {new: true})
.then(data => console.log(data))
Student.findOneAndDelete({roll: 'X'})
.then(msg => console.log(msg))
//Methods
const findData = async () => {
const foundData = await Student.find({name: 'Swapnil'});
console.log(foundData);
}
findData();
const removeAll = async() => {
const msg = await Student.deleteMany({});
console.log(msg);
}
removeAll();
//Instance Methods
Student.insertMany({name: 'Jimmy', roll: 6, gender: 'Other'});
dataSchema.methods.addStd = function () {
this.std = 'V';
return this.save();
}
const findData = async () => {
const foundData = await Student.findOne({name: 'Jimmy'});
console.log(foundData);
foundData.addStd((data, err) => {
console.log(data);
})
}
findData();
//Static method
dataSchema.statics.cleanRoll = function () {
return this.updateMany({}, {roll: 0});
};
Student.cleanRoll().then(res => console.log(res))