What are Mongoose middleware hooks?
Pre and post hooks that run before/after document operations. pre('save') is commonly used for password hashing. post('save') for sending emails. pre('find') for query modification. pre('remove') for cleanup. They let you add behavior without modifying the main logic.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Node.js MongoDB and Mongoose Interview Questions Schemas, Indexes, and Aggregation
MongoDB is a NoSQL database that stores JSON-like documents. Mongoose is an ODM (Object Data Modeling) library for MongoDB in Node.js that adds schema definition, validation, type casting, middleware hooks, population (ref), and query building.
Indexes create a sorted data structure that speeds up queries. Without indexes, MongoDB scans every document (collection scan). With indexes, MongoDB uses the index (index scan). Types: single field, compound (multiple fields), unique (no duplicates), and text (for search).
ref is a schema option that references documents in another collection (stores ObjectId). populate replaces the ObjectId with the actual document when querying: Post.find().populate('author', 'name email'). This is like a JOIN in SQL.
Still have questions?
Browse all our FAQs or reach out to our support team
