CRUD Operations: Distinct
🚀 Goal: Discover Unique Values Like a Pro
Your platform is evolving, and now it’s time to give your users the power to filter and explore listings in new ways. Imagine a guest searching for properties by neighborhood, amenities, or property type—dynamic filters make it all possible. As the backend engineer, you’ll use MongoDB’s distinct
to unlock these features and make your app truly interactive.
In this exercise, you’ll reveal all the unique values in any field, powering up the filters that help guests find exactly what they’re looking for. This is how you turn a simple list into a personalized discovery experience.
đź§© Exercise: Find Unique Values
-
Open the File
Navigate toserver/src/lab/
and opencrud-3.lab.js
. -
Locate the Function
Find thecrudDistinct
function in the file. -
Shape the Query
- Use the
distinct
method to retrieve all unique values for the field specified by thefield_name
parameter. - Return an array of all distinct values found in that field across the collection.
- Use the
🚦 Test Your API
- Go to
server/src/lab/rest-lab
. - Open
crud-3-lab.http
. - Click Send Request to execute the API call.
- Verify the response returns a list of unique values for the requested field.
🖥️ Frontend Validation
Open the “Filters” panel in the app and watch as all distinct values for your chosen field appear—enabling dynamic, user-friendly filtering for your guests.
With this step, you’re not just fetching data—you’re empowering your users to discover the perfect stay, their way.
Ready to make your platform smarter and more interactive? Let’s get started!