-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBMS Assignment 11
More file actions
633 lines (628 loc) · 13.7 KB
/
DBMS Assignment 11
File metadata and controls
633 lines (628 loc) · 13.7 KB
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
Title: Aggregation and indexing with suitable example using MongoDB.
Problem Statement: Create an orders collection with keys order_id, cust_id, cust_name, phone_no(array field), email_id(optional field), item_name, DtOfOrder, quantity, amount, status(P :pending/D:delivered)
db.orders.find().pretty()
{
"_id" : ObjectId("59c0b58afbaf678ecfddb005"),
"Order_id" : 1,
"Cust_id" : "A1",
"Cust_name" : "Aryan",
"Phone_no" : [
9890151243,
8806048721
],
"Email_id" : "aryan@gmail.com",
"Item_name" : "Laptop",
"DtOfOrder" : ISODate("2017-06-12T00:00:00Z"),
"Qty" : 2,
"Amt" : 90000,
"Status" : "D"
}
{
"_id" : ObjectId("59c0b5f7fbaf678ecfddb006"),
"Order_id" : 2,
"Cust_id" : "B1",
"Cust_name" : "Carol",
"Phone_no" : [
9860151243,
8806048723
],
"Item_name" : "Watch",
"DtOfOrder" : ISODate("2017-02-12T00:00:00Z"),
"Qty" : 3,
"Amt" : 40000,
"Status" : "P"
}
{
"_id" : ObjectId("59c0b63afbaf678ecfddb007"),
"Order_id" : 3,
"Cust_id" : "C1",
"Cust_name" : "Sam",
"Phone_no" : [
9960151243,
8706048723
],
"Item_name" : "Mobile",
"DtOfOrder" : ISODate("2017-02-12T00:00:00Z"),
"Qty" : 2,
"Amt" : 30000,
"Status" : "P"
}
{
"_id" : ObjectId("59c0b698fbaf678ecfddb008"),
"Order_id" : 4,
"Cust_id" : "A1",
"Cust_name" : "Aryan",
"Phone_no" : [
9890151243,
8806048721
],
"Email_id" : "aryan@gmail.com",
"Item_name" : "Belt",
"DtOfOrder" : ISODate("2017-09-12T00:00:00Z"),
"Qty" : 3,
"Amt" : 5000,
"Status" : "P"
}
{
"_id" : ObjectId("59c0b6eafbaf678ecfddb009"),
"Order_id" : 5,
"Cust_id" : "B1",
"Cust_name" : "Carol",
"Phone_no" : [
9860151243,
8806048723
],
"Item_name" : "T-shirt",
"DtOfOrder" : ISODate("2017-08-22T00:00:00Z"),
"Qty" : 6,
"Amt" : 12000,
"Status" : "D"
}
{
"_id" : ObjectId("59c0b717fbaf678ecfddb00a"),
"Order_id" : 6,
"Cust_id" : "C1",
"Cust_name" : "Sam",
"Phone_no" : [
9960151243,
8706048723
],
"Item_name" : "Jio Router",
"DtOfOrder" : ISODate("2017-02-12T00:00:00Z"),
"Qty" : 3,
"Amt" : 6000,
"Status" : "P"
}
1. Create a simple index on cust_id and also create a simple index on item_name.Try to make a duplicate entry.
db.orders.ensureIndex({Cust_id:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.orders.ensureIndex({Item_name:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
> db.orders.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "order.orders"
},
{
"v" : 1,
"key" : {
"Cust_id" : 1
},
"name" : "Cust_id_1",
"ns" : "order.orders"
},
{
"v" : 1,
"key" : {
"Item_name" : 1
},
"name" : "Item_name_1",
"ns" : "order.orders"
}
]
2. Create unique index on the order_id key and try to make duplicate entry.
db.orders.ensureIndex({Order_id:1},{unique:true})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 3,
"numIndexesAfter" : 4,
"ok" : 1
}
> db.orders.insert({Order_id:3})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: order.orders index: Order_id_1 dup key: { : 3.0 }"
}
})
3. Create a multikey index on phone_no and find the customers with 2 phone nos.
db.orders.ensureIndex({Phone_no:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 4,
"numIndexesAfter" : 5,
"ok" : 1
}
> db.orders.find({Phone_no:{$size:2}}).pretty()
{
"_id" : ObjectId("59c0b58afbaf678ecfddb005"),
"Order_id" : 1,
"Cust_id" : "A1",
"Cust_name" : "Aryan",
"Phone_no" : [
9890151243,
8806048721
],
"Email_id" : "aryan@gmail.com",
"Item_name" : "Laptop",
"DtOfOrder" : ISODate("2017-06-12T00:00:00Z"),
"Qty" : 2,
"Amt" : 90000,
"Status" : "D"
}
{
"_id" : ObjectId("59c0b5f7fbaf678ecfddb006"),
"Order_id" : 2,
"Cust_id" : "B1",
"Cust_name" : "Carol",
"Phone_no" : [
9860151243,
8806048723
],
"Item_name" : "Watch",
"DtOfOrder" : ISODate("2017-02-12T00:00:00Z"),
"Qty" : 3,
"Amt" : 40000,
"Status" : "P"
}
{
"_id" : ObjectId("59c0b63afbaf678ecfddb007"),
"Order_id" : 3,
"Cust_id" : "C1",
"Cust_name" : "Sam",
"Phone_no" : [
9960151243,
8706048723
],
"Item_name" : "Mobile",
"DtOfOrder" : ISODate("2017-02-12T00:00:00Z"),
"Qty" : 2,
"Amt" : 30000,
"Status" : "P"
}
{
"_id" : ObjectId("59c0b698fbaf678ecfddb008"),
"Order_id" : 4,
"Cust_id" : "A1",
"Cust_name" : "Aryan",
"Phone_no" : [
9890151243,
8806048721
],
"Email_id" : "aryan@gmail.com",
"Item_name" : "Belt",
"DtOfOrder" : ISODate("2017-09-12T00:00:00Z"),
"Qty" : 3,
"Amt" : 5000,
"Status" : "P"
}
{
"_id" : ObjectId("59c0b6eafbaf678ecfddb009"),
"Order_id" : 5,
"Cust_id" : "B1",
"Cust_name" : "Carol",
"Phone_no" : [
9860151243,
8806048723
],
"Item_name" : "T-shirt",
"DtOfOrder" : ISODate("2017-08-22T00:00:00Z"),
"Qty" : 6,
"Amt" : 12000,
"Status" : "D"
}
{
"_id" : ObjectId("59c0b717fbaf678ecfddb00a"),
"Order_id" : 6,
"Cust_id" : "C1",
"Cust_name" : "Sam",
"Phone_no" : [
9960151243,
8706048723
],
"Item_name" : "Jio Router",
"DtOfOrder" : ISODate("2017-02-12T00:00:00Z"),
"Qty" : 3,
"Amt" : 6000,
"Status" : "P"
}
4. Create a sparse index on email_id key and show the effects with and without indexing.(Hint:use find() before and after aplying index.Also use .explain())
db.orders.find({Email_id:"aryan@gmail.com"}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "order.orders",
"indexFilterSet" : false,
"parsedQuery" : {
"Email_id" : {
"$eq" : "aryan@gmail.com"
}
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"Email_id" : {
"$eq" : "aryan@gmail.com"
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "student",
"port" : 27017,
"version" : "3.2.16",
"gitVersion" : "056bf45128114e44c5358c7a8776fb582363e094"
},
"ok" : 1
}
> db.orders.ensureIndex({Email_id:1},{sparse:true})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 5,
"numIndexesAfter" : 6,
"ok" : 1
}
> db.orders.find({Email_id:"aryan@gmail.com"}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "order.orders",
"indexFilterSet" : false,
"parsedQuery" : {
"Email_id" : {
"$eq" : "aryan@gmail.com"
}
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"Email_id" : 1
},
"indexName" : "Email_id_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : true,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"Email_id" : [
"[\"aryan@gmail.com\", \"aryan@gmail.com\"]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "student",
"port" : 27017,
"version" : "3.2.16",
"gitVersion" : "056bf45128114e44c5358c7a8776fb582363e094"
},
"ok" : 1
}
5. A)Display all indexes created on order collection.
db.orders.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "order.orders"
},
{
"v" : 1,
"key" : {
"Cust_id" : 1
},
"name" : "Cust_id_1",
"ns" : "order.orders"
},
{
"v" : 1,
"key" : {
"Item_name" : 1
},
"name" : "Item_name_1",
"ns" : "order.orders"
},
{
"v" : 1,
"unique" : true,
"key" : {
"Order_id" : 1
},
"name" : "Order_id_1",
"ns" : "order.orders"
},
{
"v" : 1,
"key" : {
"Phone_no" : 1
},
"name" : "Phone_no_1",
"ns" : "order.orders"
},
{
"v" : 1,
"key" : {
"Email_id" : 1
},
"name" : "Email_id_1",
"ns" : "order.orders",
"sparse" : true
}
]
B)Also show the size of indexes.
db.orders.totalIndexSize()
151552
6. A)Delete the index on cust_id.
db.orders.dropIndex({Cust_id:1})
{ "nIndexesWas" : 6, "ok" : 1 }
B)Delete all indexes.
db.orders.dropIndex("*")
{
"nIndexesWas" : 5,
"msg" : "non-_id indexes dropped for collection",
"ok" : 1
}
7. A)Find the customers without email_id.
db.orders.find({Email_id:{$exists:false}}).pretty()
{
"_id" : ObjectId("59c0b5f7fbaf678ecfddb006"),
"Order_id" : 2,
"Cust_id" : "B1",
"Cust_name" : "Carol",
"Phone_no" : [
9860151243,
8806048723
],
"Item_name" : "Watch",
"DtOfOrder" : ISODate("2017-02-12T00:00:00Z"),
"Qty" : 3,
"Amt" : 40000,
"Status" : "P"
}
{
"_id" : ObjectId("59c0b63afbaf678ecfddb007"),
"Order_id" : 3,
"Cust_id" : "C1",
"Cust_name" : "Sam",
"Phone_no" : [
9960151243,
8706048723
],
"Item_name" : "Mobile",
"DtOfOrder" : ISODate("2017-02-12T00:00:00Z"),
"Qty" : 2,
"Amt" : 30000,
"Status" : "P"
}
{
"_id" : ObjectId("59c0b6eafbaf678ecfddb009"),
"Order_id" : 5,
"Cust_id" : "B1",
"Cust_name" : "Carol",
"Phone_no" : [
9860151243,
8806048723
],
"Item_name" : "T-shirt",
"DtOfOrder" : ISODate("2017-08-22T00:00:00Z"),
"Qty" : 6,
"Amt" : 12000,
"Status" : "D"
}
{
"_id" : ObjectId("59c0b717fbaf678ecfddb00a"),
"Order_id" : 6,
"Cust_id" : "C1",
"Cust_name" : "Sam",
"Phone_no" : [
9960151243,
8706048723
],
"Item_name" : "Jio Router",
"DtOfOrder" : ISODate("2017-02-12T00:00:00Z"),
"Qty" : 3,
"Amt" : 6000,
"Status" : "P"
}
B)Find the customers who have email_id.
db.orders.find({Email_id:{$exists:true}}).pretty()
{
"_id" : ObjectId("59c0b58afbaf678ecfddb005"),
"Order_id" : 1,
"Cust_id" : "A1",
"Cust_name" : "Aryan",
"Phone_no" : [
9890151243,
8806048721
],
"Email_id" : "aryan@gmail.com",
"Item_name" : "Laptop",
"DtOfOrder" : ISODate("2017-06-12T00:00:00Z"),
"Qty" : 2,
"Amt" : 90000,
"Status" : "D"
}
{
"_id" : ObjectId("59c0b698fbaf678ecfddb008"),
"Order_id" : 4,
"Cust_id" : "A1",
"Cust_name" : "Aryan",
"Phone_no" : [
9890151243,
8806048721
],
"Email_id" : "aryan@gmail.com",
"Item_name" : "Belt",
"DtOfOrder" : ISODate("2017-09-12T00:00:00Z"),
"Qty" : 3,
"Amt" : 5000,
"Status" : "P"
}
8. Display all customer names of orders collection with no repetition
db.orders.distinct("Cust_name")
[ "Aryan", "Carol", "Sam" ]
9. A)Find Total no of orders received so far
db.orders.find({Status:'D'}).count()
2
b)how many orders are pending.
db.orders.find({Status:'P'}).count()
4
10. Show results and details of sorting documents based on amount
db.orders.find().sort({Amt:1}).pretty()
{
"_id" : ObjectId("59c0b698fbaf678ecfddb008"),
"Order_id" : 4,
"Cust_id" : "A1",
"Cust_name" : "Aryan",
"Phone_no" : [
9890151243,
8806048721
],
"Email_id" : "aryan@gmail.com",
"Item_name" : "Belt",
"DtOfOrder" : ISODate("2017-09-12T00:00:00Z"),
"Qty" : 3,
"Amt" : 5000,
"Status" : "P"
}
{
"_id" : ObjectId("59c0b717fbaf678ecfddb00a"),
"Order_id" : 6,
"Cust_id" : "C1",
"Cust_name" : "Sam",
"Phone_no" : [
9960151243,
8706048723
],
"Item_name" : "Jio Router",
"DtOfOrder" : ISODate("2017-02-12T00:00:00Z"),
"Qty" : 3,
"Amt" : 6000,
"Status" : "P"
}
{
"_id" : ObjectId("59c0b6eafbaf678ecfddb009"),
"Order_id" : 5,
"Cust_id" : "B1",
"Cust_name" : "Carol",
"Phone_no" : [
9860151243,
8806048723
],
"Item_name" : "T-shirt",
"DtOfOrder" : ISODate("2017-08-22T00:00:00Z"),
"Qty" : 6,
"Amt" : 12000,
"Status" : "D"
}
{
"_id" : ObjectId("59c0b63afbaf678ecfddb007"),
"Order_id" : 3,
"Cust_id" : "C1",
"Cust_name" : "Sam",
"Phone_no" : [
9960151243,
8706048723
],
"Item_name" : "Mobile",
"DtOfOrder" : ISODate("2017-02-12T00:00:00Z"),
"Qty" : 2,
"Amt" : 30000,
"Status" : "P"
}
{
"_id" : ObjectId("59c0b5f7fbaf678ecfddb006"),
"Order_id" : 2,
"Cust_id" : "B1",
"Cust_name" : "Carol",
"Phone_no" : [
9860151243,
8806048723
],
"Item_name" : "Watch",
"DtOfOrder" : ISODate("2017-02-12T00:00:00Z"),
"Qty" : 3,
"Amt" : 40000,
"Status" : "P"
}
{
"_id" : ObjectId("59c0b58afbaf678ecfddb005"),
"Order_id" : 1,
"Cust_id" : "A1",
"Cust_name" : "Aryan",
"Phone_no" : [
9890151243,
8806048721
],
"Email_id" : "aryan@gmail.com",
"Item_name" : "Laptop",
"DtOfOrder" : ISODate("2017-06-12T00:00:00Z"),
"Qty" : 2,
"Amt" : 90000,
"Status" : "D"
}
11. Show how many orders are placed by each customer.
db.orders.aggregate({$group:{_id:"$Cust_name",cnt_of_order:{$sum:1}}})
{ "_id" : "Sam", "cnt_of_order" : 2 }
{ "_id" : "Carol", "cnt_of_order" : 2 }
{ "_id" : "Aryan", "cnt_of_order" : 2 }
12. Display all customer ids and their total pending order amount in descending order.
db.orders.aggregate({$match:{Status:'P'}},{$group:{_id:"$Cust_id",pend_amt:{$sum:"$Amt"}}},{$sort:{pend_amt:-1}})
{ "_id" : "B1", "pend_amt" : 40000 }
{ "_id" : "C1", "pend_amt" : 36000 }
{ "_id" : "A1", "pend_amt" : 5000 }
13. Display all customer ids in ascending order with total order amount which have been is delivered.
db.orders.aggregate({$match:{Status:'D'}},{$group:{_id:"$Cust_id",tot_amt:{$sum:"$Amt"}}},{$sort:{_id:1}})
{ "_id" : "A1", "tot_amt" : 90000 }
{ "_id" : "B1", "tot_amt" : 12000 }
14. Show top three Selling Items from orders collection.
db.orders.aggregate({$group:{_id:"$Item_name",totqty:{$sum:"$Qty"}}},{$sort:{totqty:-1}},{$limit:3})
{ "_id" : "T-shirt", "totqty" : 6 }
{ "_id" : "Belt", "totqty" : 3 }
{ "_id" : "Jio Router", "totqty" : 3 }
15. Find the date on which maximum orders are received.
db.orders.aggregate({$group:{_id:"$DtOfOrder",cnt_of_order:{$sum:1}}},{$sort:{cnt_of_order:-1}},{$limit:1})
{ "_id" : ISODate("2017-02-12T00:00:00Z"), "cnt_of_order" : 3 }
16. Find which customer has placed maximum orders.
db.orders.aggregate({$group:{_id:"$Cust_name",cnt_orderid:{$sum:1}}},{$sort:{cnt_orderid:-1}},{$limit:1})
{ "_id" : "Sam", "cnt_orderid" : 2 }
Objective:
• To study indexing in mongoDb
• To study aggregation operation in MongoDb and to use various pipeline operators such as $project, $group, $match etc