QA
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→

More MongoDB & Mongoose Interview Questions

Beginner LevelMongoDB & Mongoose

How does the MongoDB Aggregation Pipeline work? Explain $match, $group, $lookup, and $project.

Beginner LevelMongoDB & Mongoose

What is MongoDB and how does the BSON document data model work?

Beginner LevelMongoDB & Mongoose

What is the structure of a MongoDB ObjectId and how is it generated?