4. Querying documents

Now that you have imported Airbnb data, you will learn to read and explore it.

The find command

To get all the documents in a collection:

db.listings.find()

This returns the first 20 documents. To see more, type it (iterate).

Searching for specific documents

You can use a filter to search for documents that meet certain conditions:

// Listings in the CIUTAT VELLA neighborhood
db.listings.find({ neighbourhood_group_cleansed: "CIUTAT VELLA" })

// Listings of type "Entire home/apt"
db.listings.find({ room_type: "Entire home/apt" })

The filter is an object with field-value pairs. MongoDB returns only the documents where those fields have those exact values.

The findOne command

To get just one document (useful for seeing the structure):

// Gets the first listing
db.listings.findOne()

// Gets a specific listing
db.listings.findOne({ room_type: "Private room" })

findOne returns a single document instead of a cursor.

Projections

You can select which fields to return using projections:

// Only returns name and price (and _id by default)
db.listings.find(
  { neighbourhood_group_cleansed: "EXTRAMURS" },
  { name: 1, price: 1 }
)

// Excludes the _id field
db.listings.find(
  { neighbourhood_group_cleansed: "EXTRAMURS" },
  { name: 1, price: 1, _id: 0 }
)

In projections: - 1 means "include this field" - 0 means "exclude this field"

You cannot mix inclusions and exclusions (except with _id).

Counting documents

You already know countDocuments from the previous lesson. Now you can use it with filters:

// Count listings in a specific neighborhood
db.listings.countDocuments({ neighbourhood_group_cleansed: "BENIMACLET" })

// Count superhosts
db.listings.countDocuments({ host_is_superhost: "t" })
Activity 1

Practice basic queries with the Airbnb listings:

  1. Find all listings in the CIUTAT VELLA neighborhood
  2. Find listings of type "Entire home/apt" (room_type)
  3. Find a single listing that is a "Private room"
  4. Show only the name, price and host_name fields of listings in POBLATS MARITIMS
  5. Count how many listings there are in the BENIMACLET neighborhood

Pro:

  1. Find listings where the host is a superhost (host_is_superhost is "t")
  2. Find a listing in the RASCANYA neighborhood and show only its name and room type
  3. Count how many "Private room" listings there are in the EXTRAMURS neighborhood (combine filters in a single object)

Solutions

Building SPAs with Django and HTML Over the Wire: Learn to build real-time single page applications with Python

Building SPAs with Django and HTML Over the Wire: Learn to build real-time single page applications with Python

The HTML over WebSockets approach simplifies single-page application (SPA) development and lets you bypass learning a JavaScript rendering framework such as React, Vue, or Angular, moving the logic to Python. This web application development book provides you with all the Django tools you need to simplify your developments with real-time results.

Buy the book

Help me keep writing

Every coffee gives me a push toward the next article.

Comments

There are no comments yet.