To fill your MongoDB database with sample data for your collections, follow these steps. I'll provide the data and instructions to populate the following collections:
- Admin (for admin users)
- Users (for regular users)
- Tours (sample tours)
- Bookings (sample bookings)
- Payments (related payments)
[
{
"name": "John Admin",
"email": "[email protected]",
"password": "securepassword123",
"role": "admin"
},
{
"name": "Jane Admin",
"email": "[email protected]",
"password": "adminpassword456",
"role": "admin"
}
]
[
{
"name": "Alice User",
"email": "[email protected]",
"password": "password123",
"role": "user"
},
{
"name": "Bob User",
"email": "[email protected]",
"password": "password456",
"role": "user"
}
]
[
{
"name": "Almaty Adventure",
"locationId": "LOC001",
"locationName": "Almaty, Kazakhstan",
"description": "Explore the beautiful city of Almaty with a guided tour.",
"price": 150,
"startDate": "2024-07-15",
"endDate": "2024-07-20",
"availableSpots": 20,
"images": ["/images/almaty1.jpg", "/images/almaty2.jpg"]
},
{
"name": "Silk Road Experience",
"locationId": "LOC002",
"locationName": "Kazakhstan, Kyrgyzstan, Uzbekistan",
"description": "A grand voyage through the historic Silk Road.",
"price": 300,
"startDate": "2024-08-10",
"endDate": "2024-08-25",
"availableSpots": 15,
"images": ["silkroad1.jpg", "silkroad2.jpg"]
},
{
"name": "Ala-Kul Lake Trek",
"locationId": "LOC003",
"locationName": "Ala-Kul Lake, Kyrgyzstan",
"description": "A trekking adventure to the stunning Ala-Kul Lake.",
"price": 200,
"startDate": "2024-09-05",
"endDate": "2024-09-10",
"availableSpots": 10,
"images": ["alakullake1.jpg", "alakullake2.jpg"]
}
]
Before inserting admins
or users
, their passwords need to be hashed because you are using bcrypt for password encryption.
Create a script to hash passwords before inserting them into the database.
Create a file named seed.js
:
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const Admin = require('./models/Admin');
const User = require('./models/User');
const Tour = require('./models/Tour');
const connectDB = async () => {
await mongoose.connect('mongodb://localhost:27017/yourdbname', {
useNewUrlParser: true,
useUnifiedTopology: true
});
console.log('MongoDB connected...');
};
const seedDB = async () => {
// Clear existing data
await Admin.deleteMany({});
await User.deleteMany({});
await Tour.deleteMany({});
// Hash passwords and add admins
const adminPassword1 = await bcrypt.hash('securepassword123', 10);
const adminPassword2 = await bcrypt.hash('adminpassword456', 10);
const admins = [
{ name: 'John Admin', email: '[email protected]', password: adminPassword1, role: 'admin' },
{ name: 'Jane Admin', email: '[email protected]', password: adminPassword2, role: 'admin' }
];
await Admin.insertMany(admins);
// Hash passwords and add users
const userPassword1 = await bcrypt.hash('password123', 10);
const userPassword2 = await bcrypt.hash('password456', 10);
const users = [
{ name: 'Alice User', email: '[email protected]', password: userPassword1, role: 'user' },
{ name: 'Bob User', email: '[email protected]', password: userPassword2, role: 'user' }
];
await User.insertMany(users);
// Add tours
const tours = [
{
name: 'Almaty Adventure',
locationId: 'LOC001',
locationName: 'Almaty, Kazakhstan',
description: 'Explore the beautiful city of Almaty with a guided tour.',
price: 150,
startDate: new Date('2024-07-15'),
endDate: new Date('2024-07-20'),
availableSpots: 20,
images: ['almaty1.jpg', 'almaty2.jpg']
},
{
name: 'Silk Road Experience',
locationId: 'LOC002',
locationName: 'Kazakhstan, Kyrgyzstan, Uzbekistan',
description: 'A grand voyage through the historic Silk Road.',
price: 300,
startDate: new Date('2024-08-10'),
endDate: new Date('2024-08-25'),
availableSpots: 15,
images: ['silkroad1.jpg', 'silkroad2.jpg']
},
{
name: 'Ala-Kul Lake Trek',
locationId: 'LOC003',
locationName: 'Ala-Kul Lake, Kyrgyzstan',
description: 'A trekking adventure to the stunning Ala-Kul Lake.',
price: 200,
startDate: new Date('2024-09-05'),
endDate: new Date('2024-09-10'),
availableSpots: 10,
images: ['alakullake1.jpg', 'alakullake2.jpg']
}
];
await Tour.insertMany(tours);
console.log('Database seeded!');
process.exit();
};
connectDB().then(seedDB);
-
Ensure you have MongoDB running locally.
-
Install dependencies if you haven't already:
npm install mongoose bcryptjs
-
Run the script:
node seed.js
Use MongoDB Compass or the MongoDB shell to verify the data.
db.admins.find()
db.users.find()
db.tours.find()
-
Admins:
- Email:
[email protected]
- Password:
securepassword123
- Email:
-
Users:
- Email:
[email protected]
- Password:
password123
- Email:
-
Tours: 3 sample tours are added.
You now have a fully populated database ready for testing. Let me know if you need further help or adjustments! 🚀