-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path28_02_2025.sql
137 lines (128 loc) Β· 2.85 KB
/
28_02_2025.sql
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
// Using the TechProducts database
use TechProductsDB
switched to db TechProductsDB
// Clear existing collections
db.Devices.drop()
true
db.Accessories.drop()
true
// Create and populate Devices collection
db.Devices.insertMany([
{
"_id": 101,
"model": "GalaxyPhone",
"price": 799,
"launchDate": ISODate("2021-05-14"),
"specifications": {
"memory": 8,
"display": 6.7,
"processor": 2.8
},
"colorOptions": ["midnight", "silver"],
"storage": [128, 256, 512]
},
{
"_id": 102,
"model": "GalaxyTab",
"price": 899,
"launchDate": ISODate("2021-09-01"),
"specifications": {
"memory": 12,
"display": 10.4,
"processor": 3.2
},
"colorOptions": ["gray", "silver", "blue"],
"storage": [256, 512]
}
])
{
acknowledged: true,
insertedIds: { '0': 101, '1': 102 }
}
// Query examples with modern syntax
// 1. Find devices with exact price match
db.Devices.find(
{ price: { $eq: 899 } },
{ model: 1, price: 1, _id: 0 }
)
{ "model": "GalaxyTab", "price": 899 }
// 2. Find devices with specific memory configuration
db.Devices.find(
{ "specifications.memory": { $gte: 8 } },
{ model: 1, "specifications.memory": 1 }
)
{
"_id": 101,
"model": "GalaxyPhone",
"specifications": { "memory": 8 }
}
{
"_id": 102,
"model": "GalaxyTab",
"specifications": { "memory": 12 }
}
// 3. Find devices available in specific colors
db.Devices.find(
{ colorOptions: { $in: ["blue", "silver"] } },
{ model: 1, colorOptions: 1 }
)
{
"_id": 101,
"model": "GalaxyPhone",
"colorOptions": ["midnight", "silver"]
}
{
"_id": 102,
"model": "GalaxyTab",
"colorOptions": ["gray", "silver", "blue"]
}
// 4. Find devices launched in 2021
db.Devices.find(
{
launchDate: {
$gte: ISODate("2021-01-01"),
$lt: ISODate("2022-01-01")
}
},
{ model: 1, launchDate: 1 }
)
{
"_id": 101,
"model": "GalaxyPhone",
"launchDate": ISODate("2021-05-14T00:00:00Z")
}
{
"_id": 102,
"model": "GalaxyTab",
"launchDate": ISODate("2021-09-01T00:00:00Z")
}
// 5. Find devices with specific storage options
db.Devices.find(
{ storage: { $all: [256, 512] } },
{ model: 1, storage: 1 }
)
{
"_id": 101,
"model": "GalaxyPhone",
"storage": [128, 256, 512]
}
{
"_id": 102,
"model": "GalaxyTab",
"storage": [256, 512]
}
// Create indexes for better performance
db.Devices.createIndex({ model: 1 })
{
"createdCollectionAutomatically": false,
"numIndexesBefore": 1,
"numIndexesAfter": 2,
"ok": 1
}
db.Devices.createIndex({ "specifications.memory": 1 })
{
"createdCollectionAutomatically": false,
"numIndexesBefore": 2,
"numIndexesAfter": 3,
"ok": 1
}