Loading

Quipoin Menu

Learn • Practice • Grow

express-js / express-js - interview
interview

Q1. What is Mongoose and why use it?
Mongoose is an ODM (Object Data Modeling) library for MongoDB and Node.js. It provides a schema-based solution to model your data, with built-in validation, type casting, and query building. It makes MongoDB interactions more structured and easier.

Q2. How do you connect Mongoose to MongoDB?
Install mongoose, then: const mongoose = require('mongoose'); mongoose.connect('mongodb://127.0.0.1:27017/mydb'). Use async/await or promises to handle connection success/error. Store the connection in a variable to reuse.

Q3. What is a Mongoose schema and model?
A schema defines the structure of documents (fields, types, validations). A model is a compiled version of the schema that provides an interface to the database collection. Example: const userSchema = new mongoose.Schema({ name: String }); const User = mongoose.model('User', userSchema);

Q4. How do you perform CRUD with Mongoose?
Create: const user = new User({name:'John'}); await user.save(). Read: await User.find() or User.findById(id). Update: await User.findByIdAndUpdate(id, {name:'Jane'}). Delete: await User.findByIdAndDelete(id).

Q5. What are Mongoose middleware (hooks)?
Middleware are functions that run before or after certain operations like save, validate, remove. They're useful for hashing passwords before saving, or populating related data. Example: userSchema.pre('save', function(next) { this.updatedAt = Date.now(); next(); });