Intermediate LevelMongoDB & Mongoose
How do automatic schema timestamps ({ timestamps: true }) operate in Mongoose?
❓ Interview Prompt: Explain Mongoose timestamp option, createdAt and updatedAt Date injection, and custom field mapping.
Setting '{ timestamps: true }' in a Mongoose schema automatically injects 'createdAt' and 'updatedAt' Date properties into documents:
Code Implementation Example
typescript
import { Schema, model } from 'mongoose';
const productSchema = new Schema({
name: String
}, {
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' }
});
export const ProductModel = model('Product', productSchema);💡Configuring custom automatic schema timestamps in Mongoose.
🎯 Interview Key Takeaway
Enable { timestamps: true } in Mongoose schemas to automatically track document creation and update dates.
#timestamps#createdAt#Mongoose#Intermediate
Back to all MongoDB & Mongoose Questions→