Link: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/
- Execute in terminal one by one the following commands.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections
- Check if works.
sudo service mongod start
- Check the log, we should see something like [initandlisten] waiting for connections on port 27017
sudo cat /var/log/mongodb/mongod.log
- Reset service.
sudo service mongod stop
- Reboot service to keep it working.
sudo service mongod restart
mongo
Link: https://docs.mongodb.com/compass/master/install/
- Download from link: https://www.mongodb.com/download-center/compass?jmp=hero
- Execute the downloaded file.
- Now you've got mongodb-compass.
- Start mongodb
sudo service mongod restart
-
Open compass
-
Create database (green button) and select the Database fields name: db_companies, Collection name: companies.
-
Import json file to mongo:
mongoimport --db db_companies --collection companies companies.json
pip3 install pymongo
Now open jupyter notebook
jupyter notebook
# imports
from pymongo import MongoClient
import pandas as pd
# connection with mongo
client = MongoClient("mongodb://localhost:27017/")
# connection with database "db_companies"
db = client.db_companies
# companies collection
collection_companies = db.companies
# querying EVERYTHING
query = collection_companies.find()
# convert to pandas.DataFrame
df = pd.DataFrame(query)
We are back with our queries! 😉
We have learned some super useful query operators, that will helps us to make much better queries to retrieve the data we need. We will continue using the Crunchbase database we used on the last exercise.
- Upon completion, run the following commands
$ git add .
$ git commit -m "done"
$ git push origin master
- Create Pull Request so your TAs can check up your work.
Since we will be querying our database from Mongo Compass, you will need to copy/paste the query
, projection
, sort
, skip
and limit
you entered on Mongo Compass. In the queries.md
file, you will find the instructions about the queries you need to do, and a field to fill the answers.
- This is an example
query
: /You should copy/paste the query in here/projection
: /You should copy/paste the projection in here/sort
: /You should copy/paste the sort in here/skip
: /You should copy/paste the skip in here/limit
: /You should copy/paste the limit in here/
First, we need to import the database we will be using for the lab
. We will use the Crunchbase database. Crunchbase is the premier destination for discovering industry trends, investments, and news about hundreds of thousands of companies globally. From startups to Fortune 500s, Crunchbase is recognized as the primary source of company intelligence by millions of users globally.
The database contains more than 18k documents, and each of them has a lot of information about each of the companies. A document looks like the following:
- You will find the
.zip
file of the Database on the lab folder. - Unzip the file
- From the terminal, import the database to Mongo using the following command:
$ mongoimport --db companies --collection companies --file companies.json
- Check on Mongo Compass if everything goes ok:
:::info >
When running the mongoimport
you should be located in the same folder as the companies.json
file.
:::
You already know how this goes, so let's start working:
- All the companies that it's name match 'Babelgum'. Retrieve only their
name
field. - All the companies that have more than 5000 employees. Limit the search to 20 companies and sort them by number of employees.
- All the companies founded between 2000 and 2005, both years included. Retrieve only the
name
andfounded_year
fileds. - All the companies that had a Valuation Amount of more than 100.000.000 and have been founded before 2010. Retrieve only the
name
andipo
fields. - All the companies that have less than 1000 employees and have been founded before 2005. Order them by the number of employees and limit the search to 10 companies.
- All the companies that don't include the
partners
field. - All the companies that have a null type of value on the
category_code
field. - All the companies that have at least 100 employees but less than 1000. Retrieve only the
name
andnumber of employees
fields. - Order all the companies by their IPO price descendently.
- Retrieve the 10 companies with more employees, order by the
number of employees
- All the companies founded on the second semester of the year. Limit your search to 1000 companies.
- All the companies that have been 'deadpooled' after the third year.
- All the companies founded before 2000 that have and acquisition amount of more than 10.000.000
- All the companies that have been acquired after 2015, order by the acquisition amount, and retrieve only their
name
andacquisiton
field. - Order the companies by their
founded year
, retrieving only theirname
andfounded year
. - All the companies that have been founded on the first seven days of the month, including the seventh. Sort them by their
aquisition price
descendently. Limit the search to 10 documents. - All the companies on the 'web'
category
that have more than 4000 employees. Sort them by the amount of employees in ascendant order. - All the companies which their acquisition amount is more than 10.000.000, and currency are 'EUR'.
- All the companies that have been acquired on the first trimester of the year. Limit the search to 10 companies, and retrieve only their
name
andacquisition
fields. - All the companies that have been founded between 2000 and 2010, but have not been acquired before 2011.
Happy Coding! ❤️