MongoDB Aggregations
🚀 Goal: Analyze Listings with Aggregations
Your platform is buzzing with activity, and your team wants to make smarter decisions—fast. Imagine being able to spot trends, set competitive prices, or help guests find the best value for their needs. As the backend engineer, you’re the data wizard who can turn mountains of raw listings into actionable insights.
In this exercise, you’ll unleash the power of MongoDB aggregations to reveal the average price of listings by number of beds. With just a few pipeline stages, you’ll transform your data into knowledge that drives your business forward.
đź§© Exercise: Craft Your Aggregation Pipeline
-
Open the File
Head toserver/src/lab/
and crack openpipeline-1.lab.js
. -
Find the Function
Locate theaggregationPipeline
function. -
Shape the Pipeline
- $match: Filter for documents with both
beds
andprice
fields. - $group: Group by number of beds and calculate the average price.
- $sort: Sort by beds (ascending).
- $project: Return only
beds
andprice
fields.
- $match: Filter for documents with both
🚦 Test Your API
- Go to
server/src/lab/rest-lab
. - Open
pipeline-1-lab.http
. - Click Send Request to hit the API.
- Check that you get the expected results—average prices by bed count!
🖥️ Frontend Validation
- Tick the “Show Statistics” checkbox to see your aggregated results come alive in the table.
With this step, you’re not just crunching numbers—you’re giving your company the insights it needs to shine in the rental market.
Ready to turn data into strategy? Let’s get started!