Overview
Built a comprehensive user registration system with a detailed Mongoose schema featuring nested objects, validations, a beautiful EJS form with multiple sections, and a complete workflow to save submitted data to MongoDB.
What I Learned Today
- Creating advanced Mongoose schemas with nested objects (
address, social)
- Using validation fields (
required, trim, unique, enum, match, lowercase)
- Server-side rendering with EJS templates
- Handling POST form data with
express.urlencoded()
- Structuring complex forms with multiple sections
- Error handling for duplicate usernames
- Using
timestamps option for automatic createdAt and updatedAt fields
Key Concepts
Mongoose Schema Design
- Nested Objects: Organize related fields into sub-objects (e.g.,
address, social)
- Validation: Mongoose automatically validates data before saving
- Unique Constraints: Prevent duplicate entries (e.g., unique username)
- Data Transformation: Use
lowercase, trim to normalize input
EJS & Form Handling
- Server-Side Rendering: EJS allows dynamic HTML generation on the server
- Form Data Parsing:
express.urlencoded({ extended: true }) is required to parse POST form data
- Structured Forms: Organize complex forms into logical sections for better UX