-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_advanced_queries.js
More file actions
181 lines (164 loc) · 5.03 KB
/
4_advanced_queries.js
File metadata and controls
181 lines (164 loc) · 5.03 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
const { connect, closeConnection } = require('./0_connection');
// Comparison Operators Examples
async function comparisonOperators() {
const db = await connect();
try {
// $eq - Equals
const exactPrice = await db.collection('products')
.find({ price: { $eq: 199.99 } })
.toArray();
// $gt, $gte - Greater than, Greater than or equal
const expensiveProducts = await db.collection('products')
.find({
price: {
$gt: 500, // price > 500
$lte: 2000 // price <= 2000
}
})
.toArray();
// $in - Match any value in array
const specificCategories = await db.collection('products')
.find({
category: {
$in: ["Electronics", "Accessories"]
}
})
.toArray();
// $ne - Not equal
const notElectronics = await db.collection('products')
.find({
category: { $ne: "Electronics" }
})
.toArray();
console.log('Comparison operator results:', {
exactPrice,
expensiveProducts,
specificCategories,
notElectronics
});
} catch (error) {
console.error('Error in comparison operators:', error);
}
}
// Logical Operators Examples
async function logicalOperators() {
const db = await connect();
try {
// $and operator
const androidPhones = await db.collection('products')
.find({
$and: [
{ category: "Electronics" },
{ "specifications.os": "Android" },
{ price: { $lt: 1000 } }
]
})
.toArray();
// $or operator
const dealItems = await db.collection('products')
.find({
$or: [
{ on_sale: true },
{ clearance: true }
]
})
.toArray();
// $nor operator - neither condition is true
const regularItems = await db.collection('products')
.find({
$nor: [
{ on_sale: true },
{ clearance: true }
]
})
.toArray();
// Complex query combining operators
const specificProducts = await db.collection('products')
.find({
$and: [
{
$or: [
{ category: "Electronics" },
{ category: "Accessories" }
]
},
{
price: { $gt: 100 }
},
{
in_stock: true
}
]
})
.toArray();
console.log('Logical operator results:', {
androidPhones,
dealItems,
regularItems,
specificProducts
});
} catch (error) {
console.error('Error in logical operators:', error);
}
}
// Sorting and Limiting Results
async function sortingAndPagination() {
const db = await connect();
try {
// Basic sorting
const sortedByPrice = await db.collection('products')
.find({})
.sort({ price: -1 }) // -1 for descending, 1 for ascending
.toArray();
// Multiple sort conditions
const complexSort = await db.collection('products')
.find({})
.sort({
category: 1, // First sort by category (ascending)
price: -1 // Then by price (descending)
})
.toArray();
// Pagination example
const pageSize = 10;
const pageNumber = 1;
const paginatedResults = await db.collection('products')
.find({})
.skip(pageSize * (pageNumber - 1))
.limit(pageSize)
.toArray();
// Combining everything
const filteredSortedPaginated = await db.collection('products')
.find({
price: { $gt: 100 },
in_stock: true
})
.sort({ price: 1 })
.skip(0)
.limit(5)
.toArray();
console.log('Sorting and pagination results:', {
sortedByPrice,
complexSort,
paginatedResults,
filteredSortedPaginated
});
} catch (error) {
console.error('Error in sorting and pagination:', error);
} finally {
await closeConnection();
}
}
// Run all examples if this file is run directly
if (require.main === module) {
async function runAll() {
await comparisonOperators();
await logicalOperators();
await sortingAndPagination();
}
runAll();
}
module.exports = {
comparisonOperators,
logicalOperators,
sortingAndPagination
};