MongoDB Bytes #4: Let's practice some basic MQL!

G’Day, Folks! :wave:

Let’s revisit MongoDB Basics and practice some questions along with it! Most of the concepts covered are targeted at beginner audiences and learners wanting to practice more queries. I have added syntax and documentation links too if you need any help.
Please feel free to comment if you face any issues or doubts when trying the queries. I’ll post detailed answers to the queries next week!

For the practice questions, we will be using the listingsAndReviews collection from the sample_airbnb database. You can use the same Atlas Cluster that you set up while doing the university courses😄 If you haven’t loaded the sample data yet, fear not! You can do so by following the steps: Load Sample Data in Atlas

The sample_airbnb.listingsAndReviews collection contains documents that represent the vacation home listing details and reviews of customers about the listing. These documents contain fields like name, property name, reviews, room type, bed type and other details related to Airbnb. You can read more about the collection as well as see a sample document from the documentation.

  1. Firstly, to get all documents in a collection and see, we use the following command:

    db.collections.find({})
    

    The limit() function in MongoDB is used to specify the maximum number of results to be returned. Only one parameter is required for this function to return the number of the desired result.

    To output a single document in our listingsAndReviews collection, we use the following command :

    use sample_airbnb
    db.listingsAndReviews.find({}).limit(1)
    
  2. Using .count():

    db.collections.find(<query>).count()
    

    returns the count of documents that would match the find() query for the collection or view. You can read more examples from the Documentation.

Now, let’s see if you can answer a few questions related to this concept. :grin:

How many documents in the listingsAndReviews collection have the number of reviews as 3?
  • 250
  • 300
  • 247
  • None of the above

0 voters

How many documents in the listingAndReviews collection have a moderate cancellation policy?
  • 1336
  • 1446
  • 1550
  • None of the above

0 voters

How many documents in the collection have a moderate cancellation policy with 2 bedrooms?
  • 550
  • 273
  • 1000
  • 481

0 voters

  1. Finding distinct values in an array: We can use the following syntax to get all the distinct values:
    db.collection.distinct(field, query, options)
    
    This finds the distinct values for a specified field across a single collection or view and returns the results in an array.
    For eg., if we want to see the different kinds of room types we have in our collection, we use the following command:
    db.listingsAndReviews.distinct("room_type")
    
    This returns the following result, showing the distinct room types we have in our collection:
    > db.listingsAndReviews.distinct("room_type")
      [ 'Entire home/apt', 'Private room', 'Shared room' ]
    
    
    Adding .length to the end of the query helps get the count of all the distinct values.
    >db.listingsAndReviews.distinct("room_type").length
     3
    
    
Calculate the number of distinct properties that are listed on the listingsAndReviews collection.
  • 30
  • 36
  • 46
  • 56

0 voters

Let’s practice some more questions. :smile:

How many documents have exactly 3 bedrooms and have a “moderate ”cancellation policy?
  • 80
  • 81
  • 82
  • 83

0 voters

What is the name of Airbnb that accommodates 12 people having 12 beds and has a moderate cancellation_policy?
  • Le Bed & Cocktail: Discover Montreal like a local
  • Be Happy in Porto
  • 3 chambres au coeur du Plateau
  • None of the above

0 voters

How many documents have wifi as an amenity?
  • 5303
  • 5000
  • 6023
  • None of the above

0 voters

What is the name of the property that is a Resort and has 3 reviews?
  • Ribeira Charming Duplex
  • Wyndham Kona Hawaiian Resort - Two Bedroom Condo WVR
  • Private Room in Bushwick
  • Marriott Waiohai Beach Club 2Bd Villa 2020

0 voters

Which of the following is NOT a bed_type in the collection?
  • Couch
  • Real Bed
  • Airbed
  • Bunk Bed

0 voters

Do try these and as already said, please feel to post any doubts or queries you face while trying the questions! Also. we’ll post the solutions when the participation in the quiz crosses 30 folks, so keep an eye out for the solutions too! :smile:

Regards,
Satyam

9 Likes