CRUD Operations: Find/Query
🚀 Goal: Advanced Filtering & Pagination Like a Pro
Your platform is now buzzing with listings, and your users want to find their perfect stay—fast. Imagine a guest searching for a cozy apartment with a hot tub, or a family looking for a house with just the right number of beds. As the backend engineer, it’s your job to make these searches seamless and powerful.
In this exercise, you’ll combine multiple filters and pagination to create a dynamic, user-friendly search experience. With MongoDB, you’ll let guests filter by amenities, property type, and bed count—returning only the listings that match their dreams.
đź§© Exercise: Find Documents with Filters
-
Open the File
Navigate toserver/src/lab/
and opencrud-4.lab.js
. -
Locate the Function
Find thecrudFilter
function in the file. -
Shape the Query
- Filter by:
amenities
: array of selected amenitiespropertyType
: specific property typebeds
: range of beds (format: “2-3”, “4-7”)
- Add pagination with:
skip
: number of documents to skiplimit
: maximum documents to return
- No filters? Return all documents (with pagination).
- Filter by:
🚦 Test Your API
- Go to
server/src/lab/rest-lab
. - Open
crud-4-lab.http
. - Click Send Request to execute the API call.
- Confirm the response returns documents matching your filters.
🖥️ Frontend Validation
Set different filters in the app’s “Filters” panel and watch your listings update in real time—fast, flexible, and user-friendly! This is the search experience that keeps guests coming back, and it’s all powered by your code.
With this step, you’re not just filtering data—you’re helping every guest find their perfect stay, no matter what they’re looking for.
Ready to make your platform truly dynamic? Let’s get started!