backend question
backend question
Database (MongoDB)
1. What is the difference between SQL and NoSQL databases?
o SQL: Structured, uses tables.
o NoSQL: Flexible schema, uses collections and documents.
2. What are MongoDB collections and documents?
o Collection: A group of documents (like a table).
o Document: A JSON-like object (like a row).
3. Explain find(), findOne(), and aggregate().
o find(): Retrieves multiple documents.
o findOne(): Retrieves a single document.
o aggregate(): Performs complex queries like joins or grouping.
4. How do you implement relationships in MongoDB?
o Embedding: Storing related data within the same document.
o Referencing: Storing references to related documents.
5. What are indexes in MongoDB?
Indexes improve query performance by creating a data structure to
quickly locate data.
db.collection.createIndex({ field: 1 });
Performance Optimization
1. How do you optimize queries in MongoDB?
Use indexes, avoid unnecessary data fetching, and use aggregation.
2. Explain caching in a web application.
Store frequently accessed data temporarily (e.g., using Redis).
3. What are best practices for optimizing Express apps?
o Use gzip compression.
o Optimize middleware.
o Use load balancing.
4. How do you use tools like pm2?
pm2 manages Node.js processes for reliability.
5. pm2 start app.js
6. What is connection pooling in MongoDB?
Reusing connections instead of creating new ones improves
performance.
Real-World Scenarios
1. How would you scale a backend for high traffic?
o Load balancing.
o Horizontal scaling.
o Caching.
2. How do you handle rate limiting?
Use libraries like express-rate-limit.
3. How would you handle large file uploads?
Use streaming and services like S3 for storage.
4. How would you migrate a MongoDB schema?
Use scripts or tools like Mongoose for controlled migration.
5. How do you ensure APIs are fault-tolerant?
Implement retries, fallback mechanisms, and proper error handling.