-
Notifications
You must be signed in to change notification settings - Fork 0
/
module1.ts
383 lines (262 loc) · 10.4 KB
/
module1.ts
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
{
/*
# MongoDB installation process and also install compass and No SQL Booster which is the UI Interface .
1. video exist in programming hero next level web development module 4
2. second video in how to install mongo DB, mongo DB shell and No SQL Booster which is the GUI UI data representation0
## ****5-2 insert,insertOne, find, findOne, field filtering, project****
- [ ] db.test.insertMany([{name:'complete web development'},{name:'Next Level Web Development'}]); insert many data at a time .
- [ ] db.test.find({}); find all data commands
- [ ] db.test.find({age:17}); find specific data with using https://github.com/Apollo-Level2-Web-Dev/mongodb-practice/blob/main/practice-data.json
- [ ] db.test.find({company:'Demimbu'});
- [ ] db.test.find({gender:"Male"})
- [ ] db.test.find({gender:"Male"},{name:1,email:1,address:1}); // specific user needed data gathering process . // field filtering process
- [ ] db.test.find({gender:"Female"}).project({name:1,email:1,address:1}); find filtering process
# ****5-3 $eq, $neq, $gt, $lt, $gte, $lte****
- [ ] https://www.mongodb.com/docs/manual/reference/operator/query/eq/#mongodb-query-op.-eq
- [ ] db.test.find( {gender: { $eq:'Male' }}); // equal operator
- [ ] db.test.find( {age: { $ne:12 }}); // not equal operator
- [ ] db.test.find({age:{$gt:30}}); // gather then operator
- [ ] db.test.find({age:{$gte:30}}).sort({ age:1 }); gather then and equal with sorting —→ ascending order;
- [ ] db.test.find({age:{$lte:30}}).sort({ age:1 }); less then and equal with sorting —→ ascending order;
- [ ]
## ****5-4 $in, $nin, implicit and condition****
- [ ] db.test.find({age:{$lt:18}}).sort({ age:1 });
- [ ] db.test.find({age:{$gt:18,$lt:30}},{age:1}).sort({ age:1 }); // less then 18 to gather then 30 with only need age data also ascending order sorting .
- [ ] db.test.find({gender:{$eq:'Female'},age:{$gt:18,$lt:30}},{age:1,gender:1}).sort({ age:1 }); //implicate And condition
- [ ] db.test.find({gender:{$eq:'Female'},age:{$in:[18,24,28,36]}},{age:1,gender:1}).sort({ age:1 }); //implicate And condition // in operator working with like or condition if any thing match this operator will be return the document ;
- [ ] db.test.find({gender:{$eq:'Female'},age:{$nin:[18,24,28,36]}},{age:1,gender:1}).sort({ age:1 }); // nin not equal operator
- [ ] db.test.find(
{
gender:{$eq:'Female'},
age:{$nin:[18,24,28,36]},
interests:'Cooking'
```
},{age:1,gender:1,interests:1}).sort({ age:1 });
```
//implicit And
- [ ]
db.test.find(
{
gender:{$eq:'Female'},
age:{$nin:[18,24,28,36]},
interests:{$in:['Cooking','Gaming']}
```
},{age:1,gender:1,interests:1}).sort({ age:1 });
```
//implicit And
## **5-5 $and, $or, implicit vs explicit**
- // db.test.find({age:{$ne:26,$lte:30}}).projection({age:1})
db.test.find({$and:[
{age:{$ne:26}},
{age:{$lte:30}},
```
]},{age:1}); // and opertaor
```
- [ ]
db.test.find({$and:[
{age:{$ne:26}},
{age:{$lte:30}},
{gender:'Female'}
```
]},{age:1,gender:1}).sort({ age:1}); //and opertaor
```
- [ ]
db.test.find({
$or:[
{interests:'Gardening'},
{interests:'Travelling'}
]
})
.projection({interests:1});
- [ ] Array of Object handeling in mongodb
db.test.find({
$or:[
{"[skills.name](http://skills.name/)":"JAVASCRIPT"},
{"[skills.name](http://skills.name/)":"JAVA"}
```
]
```
})
.projection({ skills:1})
- [ ] db.test.find({
"[skills.name](http://skills.name/)":{$in:['JAVASCRIPT','PYTHON']}
})
.projection({ skills:1})
- [ ] db.test.find({
$or:[
{'[skills.name](http://skills.name/)':'JAVASCRIPT'},
{'[skills.name](http://skills.name/)':'PYTHON'},
]
})
.projection({ skills:1})
# ****5-6 $exists, $type,$size****
db.test.find({
phone:{$exists:true}
}) // exists property in mongodb
Type :
Type operator
db.test.find({age:{$type: 'number'}});
SIZE Operator
db.test.find({friends:{$size: 4}})
.projection({friends:1});
NULL Operator
db.test.find({company:{$type: "null"}})
.projection({company:1});
# ****5-7 $all , $elemMatch****
1. db.test.find({'interests.2' :'Cooking'}).project({interests:1}); // indexing data searching process
//I have to search second index all data for interests.
1. db.test.find({interests:{$all:["Travelling", "Reading", "Cooking"]}});
if(ravelling" && "Reading", && "Cooking){} it is working like and operator
all operator working like && operator
1. db.test.find({skills:{$elemMatch: {name:'JAVASCRIPT',level:'Intermidiate'}}}).project({skills:1}); elemMatch : **There is no problem of position, just match(পজিশনের কোন সমস্যা নাই শুধু ম্যাচ করলেই হবে) এক্ষেত্রে আমরা সবসময় ম্যাচ এলিমেন্ট ইউজ করতে পারি**
# ****5-8 $set, $addToSet, $push****
1.https://www.mongodb.com/docs/manual/reference/operator/update/set/
1. //update document
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000065")},{$set:{
```
age:'55'
```
}}); have a problem the $set operator can not be update array, object or array of object type data;
3.https://www.mongodb.com/docs/manual/reference/operator/update-array/
new data added property $addToSet duplicate value can not be accept easily identify the data redundancy
//update document
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000065")},{$addToSet:{
interests:'Writting'
}});
1. https://www.mongodb.com/docs/manual/reference/operator/update/addToSet/
$each operator —> মাল্টিপল এরে অথবা অবজেক্ট একসাথে আপডেট করতে চাইলে আমরা each operator use .
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000065")},{$addToSet:{
interests:{$each: ['sohel','rana','ali']}
}}); this operator duplicate value can not be accept easily identify the data redundancy
5.
//update document
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000065")},{$push:{
interests:{$each: ['sohel']}
}});
$push operator working with duplicate value
# ****5-9 $unset, $pop, $pull, $pullAll****
https://www.mongodb.com/docs/manual/reference/operator/update/unset
1. specific documentation data deleting process
//update document
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000065")},{
$unset:{birthday:""}
});
1. https://www.mongodb.com/docs/manual/reference/operator/update/pop/
//update document last index
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000065")},{
$pop: {interests:1}
});
//update document first index
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000065")},{
$pop: {interests:-1}
});
1. https://www.mongodb.com/docs/manual/reference/operator/update/pull/
//update document
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000065")},{
$pull: {interests:'Writting'}
});
1. https://www.mongodb.com/docs/manual/reference/operator/update/pullAll/
//update document
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000065")},{
$pullAll: {interests:["sohel","rana"]}
});
# ****5-10 More about $set, how to explore documentation****
1. update object data use $set operator
//update document
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000065")},{
$set:{"address.city":'Dhaka'}
});
1. Multiple object Updating process
//update document
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000065")},{
$set:{"address.city":'Dhaka',
"address.country":'Bangladesh',
"address.postalCode":"00017852"
}
});
1. Update array of object property
https://www.mongodb.com/docs/manual/reference/operator/update/positional/
//update document
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000065"),"education.major":"Art"},{
$set:{
"education.$.major":"Mathematices"
}
});
1. Increment data : https://www.mongodb.com/docs/manual/reference/operator/update/inc/
//update document
//{ $inc: { quantity: -2, "metrics.orders": 1 } }
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000065")},{
$inc: {
age: 1
}
});
# ****5-11 delete documents, drop collection and how to explore by yourself****
https://www.w3schools.com/mongodb/mongodb_mongosh_create_collection.php
create collection process : db.createCollection("posts")
https://www.mongodb.com/docs/manual/reference/method/db.collection.drop/
drop collection
## Precise Modules
1.problem file url : https://docs.google.com/document/d/1VBpfx8k1ZFck21A2fkXfwZD0sOww998bixeHcqNEa8Q/edit
1. solution of problem no 1 is
//pretices module
db.test.find({age:{$gt:30}}).project({name:1,email:1});
1. solution of problem no 2 is
db.test.find({$or: [
{favoutiteColor:"Maroon"},
{favoutiteColor:"Blue"}
]}).project({name:1,email:1, phone:1,favoutiteColor:1}); //solution type 1
db.test.find({favoutiteColor:{$in:[ "Blue" ,"Maroon"]}}).project({
name:1,email:1, phone:1,favoutiteColor:1
}); // solution no 2
// solution 1 and solution 2 both are return same result;
4 . solution of problem no 3 is
db.test.find({skills:{$exists: true,$size: 0}});
1. solution of problem on 4 is
db.test.find({$and: [
{'[skills.name](http://skills.name/)':'JAVASCRIPT'},
{'[skills.name](http://skills.name/)':'JAVA'}
]}).project({name:1,skills:1});
//Alternative solution same problems
db.test.find({skills:{$all: [
{"$elemMatch":{name:'JAVASCRIPT'}},
{"$elemMatch":{name:'JAVA'}}
]}}).project({name:1,skills:1});
both solution are return same result;
1. solution no 5 // all time push duplicate value can not checked data redundancy
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000065")},
//{"name": "Python", "level": "Beginner", "isLearning": true}
{$push:{
skills:{$each: [{name:'Python',level:'Beginner',isLearning:true}]}
}}
//Alternative solution same problems —→ data duplicate data redundancy checked
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000065")},
//{"name": "Python", "level": "Beginner", "isLearning": true}
{$addToSet:{
skills:{$each: [{name:'Python',level:'Beginner',isLearning:true}]}
}}
)
1. solution no 6
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000065")},
//{"name": "Python", "level": "Beginner", "isLearning": true}
{$addToSet:{
languages:{$each: ["Spanish" ]}
}}
) ;
//Alternative solution same problems —→ data duplicate data redundancy checked
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000065")},
//{"name": "Python", "level": "Beginner", "isLearning": true}
{$push:{
languages:{$each: ["Spanish" ]}
}}
) ;
7.solution No: 7
db.test.updateOne({_id:ObjectId("6406ad63fc13ae5a40000066")},
//{"name": "Python", "level": "Beginner", "isLearning": true}
{$pull: {
skills:{name:{$in:['KOTLIN']}}
```
}}
```
) ;
*/
}