MongoDB Aggregation in Python

MongoDB Aggregation in Python

Overview

In this unit, you'll learn how to perform basic aggregation with Python. First, you'll learn what aggregation is and explore the components of an aggregation pipeline. Next, you'll learn how to build a pipeline that uses the $match and $group stages. Finally, you'll explore the $sort and $project stages and build a pipeline that uses them.

Lesson 01: Building a MongoDB Aggregation Pipeline in Python Applications

  1. What is the aggregation framework used for? (Select one.)

    • A. To process documents and return computed results.

    • B. To create database schemas.

    • C. To create basic CRUD commands.

    • D. To create serverless functions.

  2. Which component(s) of an aggregation pipeline do documents pass through for processing in sequence? (Select one.)

    • A. Aggregation stages

    • B. Aggregation operators

    • C. The db.collection.aggregate() command

Lesson 02: Using MongoDB Aggregation Stages with Python: $match and $group

  1. You want to find airbnb listings for an entire home or apartment, grouped by the number of bedrooms and the average price. Given the sample document from the listingsAndReviews collection below, which of the following options should you use as the first pipeline stage? (Select one.)

     {
       "_id": "10116256",
       "listing_url": "https://www.airbnb.com/rooms/10116256",
       "name": "GOLF ROYAL RESIDENCE SUİTES(2+1)-2",
       "summary": "A BIG BED ROOM WITH A BIG SALOON INCLUDING A NICE BALAKON TO HAVE SOME FRESH AIR . OUR RESIDENCE SITUATED AT THE CENTRE OF THE IMPORTANT MARKETS SUCH AS NİŞANTAŞİ,OSMANBEY AND TAKSIM SQUARE,",
    
     ....
       "room_type": "Entire home/apt",
       ...
       "bedrooms": 2,
     ...
        "price": {
         "$numberDecimal": "997.00"
       }}
    

    A.

     {"$group": {"_id": "$bedrooms", "avg_price": {"$avg": "$price"}}
    

    B.

     {"$match": {"room_type": "Entire home/apt"}}
    

    C.

      {"$group": {"room_type": "Entire home/apt"}}
    

    D.

     {"$match": {"bedrooms": "price"}}
    

    A. Option A

    B. Option B

    C. Option C

    D. Option D

  2. You are building an aggregation pipeline to find airbnb listings for an entire home or apartment, grouped by the number of bedrooms and the average price. You have successfully built the first $match stage, as seen below. Which option below will allow you to complete the pipeline in a second stage? (Select one.)

     # Calculate the average price by number of bedrooms for every listing which is an entire home or apartment.
    
     # Select listings for the entire home or apartment. 
     entire_home = {"$match": {"room_type": "Entire home/apt"}}
    
     # Separate documents by number of bedrooms and calculate the average price.
     avg_price_by_bedrooms = 
    
     # Create an aggegation pipeline using 'stage_match_balance' and 'stage_group_account_type'.
     pipeline = [
         entire_home,
         avg_price_by_bedrooms,
     ]
    
     # Perform an aggregation on 'pipeline'.
     results = listingsAndReviews_collection.aggregate(pipeline)
    

    A.

     {"$group": {"_id": "$bedrooms", "avg_price": {"$avg": "$price"}}}
    

    B.

     {"$match": {"room_type": "Entire home/apt"}}
    

    C.

     {"$group": {"_id": "$price", "avg_price": {"$avg": "$bedrooms"}}
    

    D.

     {"$match": {"bedrooms": "price"}}
    

    A. Option A

    B. Option B

    C. Option C

    D. Option D

Lesson 03: Using MongoDB Aggregation Stages with Python: $sort and $project

  1. You've written an aggregation pipeline that matches all airbnb listings for entire homes, groups by number of bedrooms and determines the average price. Your final step in this pipeline is to return the results in ascending order, so the lowest average price is returned first. Given the code below, which option should you use to complete the sort stage? (Select one.)

    ```apache

    Calculate the average price by number of bedrooms for every listing which is an entire home or apartment, then sort in ascending order.

    Select listings for the entire home or apartment.

    entire_home = {"$match": {"room_type": "Entire home/apt"}}

    Separate documents by number of bedrooms and calculate the average price.

    avg_price_by_bedrooms = {"$group": {"_id": "$bedrooms", "avg_price": {"$avg": "$price"}}

    Sort in ascending order, to view the lowest average price first

    sort_ascending =

Create an aggegation pipeline using 'stage_match_balance' and 'stage_group_account_type'.

pipeline = [ entire_home, avg_price_by_bedrooms, sort_ascending ]

Perform an aggregation on 'pipeline'.

results = listingsAndReviews_collection.aggregate(pipeline)


    **A.**

    ```json
    {"$limit": 1}

B.

    {"$sort": {"avg_price": -1}

C.

    {"$sort": {"avg_price": 1}

D.

    {"$project" : {"avg_price" : 1}

A. Option A

B. Option B

C. Option C

D. Option D

  1. You've identified that you want to stay in an entire home with 3 bedrooms. In this new pipeline, you need to add one stage that does the following:

    • return the description and listing_url fields

    • calculate a new total_price field that adds the price and cleaning fee

    • suppress the _id field

Which option below will complete the second stage? (Select one.)

    # Select listings for an entire home with 3 bedrooms. Return the description, listing url,calculate the total price (price + cleaning fee), and suppress the _id field. 

    # Select listings for the entire home or apartment. 
    entire_home = {"$match": {"room_type": "Entire home/apt", "bedrooms": 3}}

    # Return the description, listing url, calculate the total price (price + cleaning fee), and suppress the _id field. 
    return_listings = 


    # Create an aggegation pipeline using 'stage_match_balance' and 'stage_group_account_type'.
    pipeline = [
        entire_home,
       return_listings
    ]

    # Perform an aggregation on 'pipeline'.
    results = listingsAndReviews_collection.aggregate(pipeline)

A.

    {"$project": {"description": 1,"listing_url": 1,"_id": 0,"total_price": {"$sum": ["$price", "$cleaning_fee"]}}}

B.

    {"$project": { description: 0,listing_url: 0}}

C.

     {"$return": {description: 1,listing_url: 1,_id: 0,total_price: {$sum: ['$price', '$cleaning_fee']}}}

D.

    {"$return": { description:0 ,listing_url: 0}}

A. Option A

B. Option B

C. Option C

D. Option D