Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

MongoDB - Aggregations & Projections

Introduction

We continue with MongoDB querying. Going into more detail on aggregations and projections. You will learn how to use MongoDB's aggregation framework to filter, project, sort, and limit documents in a collection.

Pre-requisites

For this exercise we will only need MongoDB and the UI:

docker compose up -d mongo mongo-express

Exercise

Part 1 - Setup

Load the data from the listingsAndReviews.json file into the airbnb database by executing the python script load_data.py. Check the data in the listingsAndReviews collection, either using the MongoDB shell or the UI.

Part 2 - Working with Aggregations and Projections

In this exercise, you will work with the listingsAndReviews collection in the airbnb database. The goal is to find the first document with the country "Brazil".

The goal is to build the aggregation pipeline to query the following requirements:

  • Match documents where the address.country is "Brazil".
  • Project only the name, address.country, and address.city fields.
  • Sort the documents in ascending order by the name field.
  • Limit the result to the first document.

There are different ways to achieve this:

  1. Use the find method with projection and sort.
  2. Use the aggregation framework with the $match, $project, $sort, and $limit stages.

Extra Exercise: Max property price

Scenario: The base price for the basic rental is $price; that price includes the number of guests provided in $guests_included field. But a property may take more guests in total ($accommodates). Those extra guests pay an $extra_people cost per person for every person more than the number of guests included.

  1. How many extra guests can each property have? Add that as a new field with $set
  2. How much it would cost with these extra guests? Use $project to show the basic price and the maximum price if fully occupied with $accommodates people

Resources