Why use .lean() and .select() in a feed API?
.lean() skips hydrating full Mongoose documents (faster, less memory). .select() limits fields to what the client needs (smaller payload). Together they make the feed API faster and lighter.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in How to Build a Feed API in Node.js
GET /feed?skip=&limit=. Exclude the current user and already-swiped users. Cap the limit. Use .lean() and .select(). Return total. Run find and countDocuments in parallel with Promise.all. Index the filter fields.
Fetch the swiped user IDs with ConnectionRequest.find({ fromUserId }).distinct('toUserId'). Then filter the feed with _id: { $nin: [req.user.userId, ...swipedIds] }. Excludes the current user and anyone already swiped on.
To prevent abuse. Without a cap, a client can request 10000 items, eating memory and bandwidth. Use Math.min(parseInt(req.query.limit) || 20, 100). Most UIs only show 20 at a time anyway.
Still have questions?
Browse all our FAQs or reach out to our support team
