Beginner LevelMongoDB & Mongoose
What is MongoDB Projection and how does it reduce network bandwidth?
❓ Interview Prompt: Explain MongoDB Projection, selecting specific fields ({ title: 1, price: 1 }), excluding fields (_id: 0), and network optimization.
Projection specifies which fields MongoDB should return in query results, reducing network payload size and Node.js memory consumption:
•
Inclusion ({ title: 1, price: 1 }): Returns ONLY specified fields (plus '_id' by default).
•
Exclusion ({ password: 0 }): Returns all fields EXCEPT specified excluded fields.
Code Implementation Example
typescript
import { UserModel } from './models/User';
async function getUserProfiles() {
// Project only name and email, excluding password and __v
const users = await UserModel.find({}, { name: 1, email: 1, _id: 0 });
return users;
}💡Executing field projection to return partial documents.
🎯 Interview Key Takeaway
Always project only required fields in queries to save network bandwidth and speed up database deserialization.
#Projection#Fields#Optimization#Beginner
Back to all MongoDB & Mongoose Questions→