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.
- Go to mongodb.com/try/download/compass
- Select your operating system
- Download and install
For Compass to work, you need to have a MongoDB server running.
- Download MongoDB Community Server from mongodb.com/try/download/community
- Follow the installation instructions for your operating system
- The server will start automatically (or run
mongod)
First steps with Compass
Once Compass is installed:
- Open MongoDB Compass
- Connect to your server (localhost or Atlas)
- You will see an interface with your databases
- 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:
- listings.csv (information on properties, hosts, prices, availability, etc.)
- reviews.csv (reviews with dates)
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.csvandreviews.csvfiles.
How to import the CSV files
There are two ways to import these files into MongoDB:
Option 1: Using MongoDB Compass (recommended for beginners)
- Download the CSV files from the links above
- Open MongoDB Compass and connect to your server
- Create a new database called
airbnb - Inside the database, create two collections:
listingsandreviews - For each collection:
- Click the "Add Data" tab and then "Import JSON or CSV file"
- Select the corresponding CSV file
- Compass will automatically detect the fields and data types
- 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: numbersMongoDB Compass can automatically convert some values on import, but
mongoimportkeeps 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
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 bookWill you buy me a coffee?
This is how I keep writing without ads or paywalls.
Sure, it's on me!
Comments
There are no comments yet.