Performance: Defining schemas and models
MEDIUM IMPACT
This affects server response time and initial data validation speed before sending data to the database.
const mongoose = require('mongoose'); const userSchema = new mongoose.Schema({ name: { type: String, required: true, trim: true }, age: { type: Number, min: 0 }, email: { type: String, required: true, unique: true, lowercase: true } }); const User = mongoose.model('User', userSchema);
const mongoose = require('mongoose'); const userSchema = new mongoose.Schema({ name: String, age: Number, email: String }); const User = mongoose.model('User', userSchema);
| Pattern | Server Validation | Database Queries | Response Time | Verdict |
|---|---|---|---|---|
| Loose schema without validation | High (manual checks needed) | More retries possible | Slower due to errors | [X] Bad |
| Strict schema with validation | Low (automatic checks) | Fewer retries | Faster and reliable | [OK] Good |