A Node.js script to fetch user data from MongoDB and export it to Excel format.
- 🔌 Connect to MongoDB (local or cloud)
- 📊 Fetch data from any collection
- 📋 List all available collections
- 📈 Export data to Excel with formatting
- 🎨 Auto-sized columns and styled headers
- 📁 Automatic file naming with timestamps
- ⚡ Support for large datasets
- 🔒 Secure connection handling
- Node.js (version 14 or higher)
- MongoDB database (local or cloud)
- MongoDB connection string
-
Clone or download this project
-
Install dependencies:
npm install
-
Configure your MongoDB connection:
- Copy
env.exampleto.env - Edit
.envand add your MongoDB connection string:
MONGODB_URL=mongodb://username:password@host:port/database_name
- Copy
Run the script:
npm startThe script will:
- Connect to your MongoDB database
- List all available collections
- Fetch data from the first collection (or you can modify to select specific)
- Export the data to an Excel file in the
exports/folder
You can modify the index.js file to:
- Select a specific collection
- Add data filtering
- Limit the number of records
- Customize the Excel filename
Example modifications:
// Select specific collection
const selectedCollection = 'users'; // Change this to your collection name
// Limit records (add to fetchData call)
const data = await exporter.fetchData(selectedCollection, 1000); // Limit to 1000 records
// Custom filename
const excelPath = await exporter.exportToExcel(data, 'my_custom_filename.xlsx');mongodb://localhost:27017/your_database
mongodb+srv://username:password@cluster.mongodb.net/database_name
mongodb://username:password@host:port/database_name?authSource=admin
The script creates an exports/ folder and saves Excel files with:
- Timestamped filenames (e.g.,
user_data_export_2024-01-15.xlsx) - Formatted headers with gray background
- Auto-sized columns
- All data fields from your MongoDB documents
The script includes comprehensive error handling for:
- Connection failures
- Invalid MongoDB URLs
- Missing collections
- Empty datasets
- File write errors
- Verify your MongoDB connection string
- Check if MongoDB is running
- Ensure network connectivity (for cloud databases)
- Verify username/password credentials
- Ensure you have read access to the database
- Check if the collection exists
- Verify database name in the connection string
- For large datasets, consider adding limits
- Monitor system memory usage
- Use pagination for very large collections
// In the fetchData method, modify the query
let query = { status: 'active' }; // Add your filter conditions
const data = await this.collection.find(query, options).toArray();// In the exportToExcel method, add custom styling
worksheet.getRow(1).font = { bold: true, size: 14 };
worksheet.getRow(1).fill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: 'FF4F81BD' }
};mongodb: MongoDB driver for Node.jsexceljs: Excel file generation librarydotenv: Environment variable management
ISC License
For issues or questions:
- Check the error messages in the console
- Verify your MongoDB connection string
- Ensure all dependencies are installed
- Check the troubleshooting section above