-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise20 noSql
74 lines (59 loc) · 1.93 KB
/
exercise20 noSql
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
EXPRIMENT NO.20 NoSQL – AGGREGATE FUNCTIONS AND REGULAR EXPRESSIONS
===================================================================
1.Write a MongoDB query to sort customer details in ascending order of their name.
==================================================================================
> db.CUSTOMER.find().sort({Name:1}).pretty()
{
"_id" : ObjectId("62a84a1ee3a5c123091c3b4f"),
"Name" : "Captain Marval",
"City" : "TVM",
"PhNo" : 8900787867,
"Age" : 26
}
{
"_id" : ObjectId("62a84a4be3a5c123091c3b50"),
"Name" : "Iron man",
"City" : "EKM",
"PhNo" : 8900787868,
"Age" : 25
}
{
"_id" : ObjectId("62a82f76c4d1000a187dd839"),
"Name" : "Krishna",
"City" : "Calicut",
"PhNo" : 8900787866,
"Age" : 32
}
{
"_id" : ObjectId("62a82fa4c4d1000a187dd83b"),
"Name" : "Tony stark",
"City" : "USA",
"PhNo" : 8900787865,
"Age" : 30
}
{
"_id" : ObjectId("62a82f94c4d1000a187dd83a"),
"Name" : "peter parker",
"City" : "USA",
"PhNo" : 8900787861,
"Age" : 28
}
``````````````````````````````````````````````````````````````````````````````````
2.Write a MongoDB query to count number of customers in each city.
=================================================================================
> db.CUSTOMER.aggregate([{$group:{_id:"$City",total:{$sum:1}}}])
{ "_id" : "Calicut", "total" : 1 }
{ "_id" : "TVM", "total" : 1 }
{ "_id" : "EKM", "total" : 1 }
{ "_id" : "USA", "total" : 2 }
>
`````````````````````````````````````````````````````````````````````````````````
3.Write a MongoDB query to find minimum and maximum age in each city
=================================================================================
> db.CUSTOMER.aggregate([{$group:{_id:"$City",Min_Age:{$min:"$Age"},Max_Age:{$max:"$Age"}}}])
{ "_id" : "EKM", "Min_Age" : 25, "Max_Age" : 25 }
{ "_id" : "Calicut", "Min_Age" : 32, "Max_Age" : 32 }
{ "_id" : "TVM", "Min_Age" : 26, "Max_Age" : 26 }
{ "_id" : "USA", "Min_Age" : 28, "Max_Age" : 30 }
>
//VERIFIED