-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueryData.js
More file actions
68 lines (59 loc) · 2.16 KB
/
Copy pathqueryData.js
File metadata and controls
68 lines (59 loc) · 2.16 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
const mongoose = require('mongoose');
// MongoDB connection
mongoose.connect('mongodb://localhost:27017/restaurant-booking', {
useNewUrlParser: true,
useUnifiedTopology: true
});
// Restaurant Schema
const restaurantSchema = new mongoose.Schema({
name: String,
cuisine: String,
address: String,
capacity: Number,
rating: Number,
image: String
});
const Restaurant = mongoose.model('Restaurant', restaurantSchema);
// Function to query and display restaurants
const queryRestaurants = async () => {
try {
// Query all restaurants
console.log('\n=== All Restaurants ===');
const allRestaurants = await Restaurant.find();
allRestaurants.forEach(restaurant => {
console.log(`\nName: ${restaurant.name}`);
console.log(`Cuisine: ${restaurant.cuisine}`);
console.log(`Address: ${restaurant.address}`);
console.log(`Capacity: ${restaurant.capacity}`);
console.log(`Rating: ${restaurant.rating}`);
});
// Query by cuisine
console.log('\n=== Italian Restaurants ===');
const italianRestaurants = await Restaurant.find({ cuisine: 'Italian' });
italianRestaurants.forEach(restaurant => {
console.log(`\nName: ${restaurant.name}`);
console.log(`Address: ${restaurant.address}`);
});
// Query high-rated restaurants (rating >= 4.8)
console.log('\n=== High-Rated Restaurants (Rating >= 4.8) ===');
const highRatedRestaurants = await Restaurant.find({ rating: { $gte: 4.8 } });
highRatedRestaurants.forEach(restaurant => {
console.log(`\nName: ${restaurant.name}`);
console.log(`Rating: ${restaurant.rating}`);
});
// Query by capacity range
console.log('\n=== Large Restaurants (Capacity >= 60) ===');
const largeRestaurants = await Restaurant.find({ capacity: { $gte: 60 } });
largeRestaurants.forEach(restaurant => {
console.log(`\nName: ${restaurant.name}`);
console.log(`Capacity: ${restaurant.capacity}`);
});
// Close the connection
await mongoose.connection.close();
console.log('\nDatabase connection closed');
} catch (error) {
console.error('Error querying database:', error);
}
};
// Run the query function
queryRestaurants();