2. Installation and MongoDB Compass

Let's set up MongoDB and its graphical tool MongoDB Compass.

Installing MongoDB Compass

MongoDB Compass is an official graphical interface for working with MongoDB. It is perfect for beginners and very useful for visualizing your data.

  1. Go to mongodb.com/try/download/compass
  2. Select your operating system
  3. Download and install

For Compass to work, you need to have a MongoDB server running.

  1. Download MongoDB Community Server from mongodb.com/try/download/community
  2. Follow the installation instructions for your operating system
  3. The server will start automatically (or run mongod)

First steps with Compass

Once Compass is installed:

  1. Open MongoDB Compass
  2. Connect to your server (localhost or Atlas)
  3. You will see an interface with your databases
  4. You can open the built-in shell from the bottom button

Run your first command in the shell:

db.version()

You should see the version of MongoDB you are using.

Practice data: Airbnb Valencia

For this course we are going to work with real Airbnb data from Valencia. This data comes from Inside Airbnb, an independent project that collects and analyzes public Airbnb data.

Available files:

Relationship between the files: The id field in listings.csv relates to listing_id in reviews.csv. This means a listing can have multiple reviews (1:N relationship).

If you want to practice with data from other cities (Barcelona, Madrid, Paris, New York, etc.), you can download it from insideairbnb.com/get-the-data. Look for the city you are interested in and download the listings.csv and reviews.csv files.

How to import the CSV files

There are two ways to import these files into MongoDB:

Option 1: Using MongoDB Compass (recommended for beginners)

  1. Download the CSV files from the links above
  2. Open MongoDB Compass and connect to your server
  3. Create a new database called airbnb
  4. Inside the database, create two collections: listings and reviews
  5. For each collection:
  6. Click the "Add Data" tab and then "Import JSON or CSV file"
  7. Select the corresponding CSV file
  8. Compass will automatically detect the fields and data types
  9. Click "Import" and wait for it to complete

Option 2: Using mongoimport from the terminal

If you prefer the command line, you can use mongoimport:

# Import listings
mongoimport --db airbnb --collection listings --type csv --headerline --file listings.csv

# Import reviews
mongoimport --db airbnb --collection reviews --type csv --headerline --file reviews.csv

Once imported, you can verify that it worked:

use airbnb
db.listings.countDocuments()
db.reviews.countDocuments()

Important note about data types: When you import CSV with mongoimport, some fields are imported as strings even though they look like numbers or booleans: - price: string (format "$XX.XX") - host_is_superhost: string ("t" or "f"), not a boolean - accommodates, number_of_reviews, review_scores_rating: numbers

MongoDB Compass can automatically convert some values on import, but mongoimport keeps the types just as they are in the CSV. This affects how you write your queries.

This data will allow us to practice realistic queries: searching for properties by price, location, number of reviews, calculating statistics by neighborhood, and much more.

This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.

Desafíos de programación atemporales y multiparadigmáticos

Desafíos de programación atemporales y multiparadigmáticos

Te encuentras ante un librillo de actividades, divididas en 2 niveles de dificultad. Te enfrentarás a los casos más comunes que te puedes encontrar en pruebas técnicas o aprender conceptos elementales de programación.

Buy the book

Will you buy me a coffee?

This is how I keep writing without ads or paywalls.

Comments

There are no comments yet.