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
import mongoose from 'mongoose';
import dotenv from 'dotenv';
import Restaurant from './backend/models/Restaurant.js';
// Load environment variables
dotenv.config();
// Sample restaurant data
const restaurants = [
{
name: "Italiano Delizioso",
description: "Authentic Italian cuisine with fresh pasta and wood-fired pizzas.",
cuisine: "Italian",
address: "123 Main St, Foodville, CA 94123",
phone: "555-123-4567",
email: "info@italianodelizioso.com",
image: "https://images.unsplash.com/photo-1517248135467-4c7edcad34c4?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1470&q=80",
rating: 4.7,
reviewCount: 128,
priceRange: 3,
deliveryTime: 35,
menu: {
categories: [
{ _id: new mongoose.Types.ObjectId(), name: "Appetizers" },
{ _id: new mongoose.Types.ObjectId(), name: "Pasta" },
{ _id: new mongoose.Types.ObjectId(), name: "Pizza" },
{ _id: new mongoose.Types.ObjectId(), name: "Desserts" }
],
items: [
{
name: "Bruschetta",
description: "Toasted bread topped with tomatoes, garlic, and fresh basil",
price: 8.99,
image: "https://images.unsplash.com/photo-1572695157366-5e585ab2b69f?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1374&q=80",
isVegetarian: true,
categoryId: mongoose.Types.ObjectId()
},
{
name: "Spaghetti Carbonara",
description: "Classic pasta with eggs, cheese, pancetta, and black pepper",
price: 16.99,
image: "https://images.unsplash.com/photo-1600803907087-f56d462fd26b?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1374&q=80",
isVegetarian: false,
categoryId: mongoose.Types.ObjectId()
}
]
}
},
{
name: "Spice Garden",
description: "Authentic Indian cuisine with a modern twist.",
cuisine: "Indian",
address: "456 Spice Ave, Foodville, CA 94123",
phone: "555-987-6543",
email: "info@spicegarden.com",
image: "https://images.unsplash.com/photo-1517248135467-4c7edcad34c4?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1470&q=80",
rating: 4.5,
reviewCount: 96,
priceRange: 2,
deliveryTime: 40,
menu: {
categories: [
{ _id: new mongoose.Types.ObjectId(), name: "Starters" },
{ _id: new mongoose.Types.ObjectId(), name: "Main Course" },
{ _id: new mongoose.Types.ObjectId(), name: "Breads" },
{ _id: new mongoose.Types.ObjectId(), name: "Desserts" }
],
items: [
{
name: "Samosas",
description: "Crispy pastry filled with spiced potatoes and peas",
price: 6.99,
image: "https://images.unsplash.com/photo-1601050690597-df0568f70950?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1470&q=80",
isVegetarian: true,
categoryId: mongoose.Types.ObjectId()
},
{
name: "Butter Chicken",
description: "Tender chicken in a rich tomato and butter sauce",
price: 18.99,
image: "https://images.unsplash.com/photo-1603894584373-5ac82b2ae398?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=1470&q=80",
isVegetarian: false,
categoryId: mongoose.Types.ObjectId()
}
]
}
}
];
// Connect to MongoDB
mongoose
.connect(process.env.MONGODB_URI)
.then(async () => {
console.log('Connected to MongoDB');
try {
// Clear existing data
await Restaurant.deleteMany({});
console.log('Cleared existing restaurant data');
// Insert new data
const createdRestaurants = await Restaurant.insertMany(restaurants);
console.log(`Added ${createdRestaurants.length} restaurants to the database`);
// Close connection
mongoose.connection.close();
} catch (error) {
console.error('Error seeding data:', error);
}
})
.catch((err) => {
console.error('MongoDB connection error:', err);
});No Output
Run the code to generate an output.