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. For this lab, we will be using the Crunchbase database. Please keep reading and work on the following iterations.
- Fork this repo
- Clone this repo
- Upon completion, run the following commands
$ git add .
$ git commit -m "done"
$ git push origin master
- Create a Pull Request and submit your assignment.
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. Each document holds the data 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
- Open MongoDB Compass and connect to your MongoDB server.
- Create a new database named
companiesDB
and inside of it a collection namedcompanies
. - Import the data from the .json file in to the collection
companies
. - Check on MongoDB Compass if everything goes ok:
You already know how this goes, so let's start working:
- All the companies whose name match 'Babelgum'. Retrieve only their
name
field.
Solution
- Query:
{name: 'Babelgum'}
- Projection:
{name: 1, _id: 0}
- All the companies that have more than 5000 employees. Limit the search to 20 companies and sort them by number of employees.
Solution
- Query:
{number_of_employees: { $gt: 5000 }}
- Limit:
20
- All the companies founded between 2000 and 2005, both years included. Retrieve only the
name
andfounded_year
fields.
Solution
- Query:
{$and: [{founded_year: {$gte: 2000}}, {founded_year:{$lte: 2005}}]}
- Projection:
{name: 1, _id: 0, founded_year: 1}
- All the companies that had a IPO Valuation Amount of more than 100,000,000 and have been founded before 2010. Retrieve only the
name
andipo
fields.
Solution
- Query:
{$and: [{'ipo.valuation_amount': {$gte: 10000000}}, {founded_year:{$lt: 2010}}]}
- Shorthand:
{founded_year: {$lt:2010}, "ipo.valuation_amount": {$gt: 100000000}}
- Shorthand:
- Projection:
{name: 1, _id: 0, ipo: 1}
- All the companies that don't include the
partners
field.
Solution
- Query:
{partners: {$exists: false}}
- All the companies that have a null type of value on the
category_code
field.
Solution
- Query:
{category_code: {$type: 'null'}}
- Order all the companies by their IPO price in descending order.
Solution
- Sort:
{'ipo.valuation_amount': -1}
- Retrieve the 10 companies with the most employees, order by the
number of employees
.
Solution
- Query:
{ number_of_employees: { $exists: true } }
- Sort:
{number_of_employees: -1}
- Limit:
10
- All the companies founded in the second semester of the year (July to December). Limit your search to 1000 companies.
Solution
- Query:
{founded_month: {$gte: 7}}
- Limit:
1000
- All the companies that have been founded on the first seven days of the month, including the seventh. Sort them by their
acquisition price
in descending order. Limit the search to 10 documents.
Solution
- Query:
{founded_day: {$lte: 7}}
- Sort:
{'acquisition.price_amount': -1}
- Limit:
10
For an extra challenge and additional practice, try the following queries:
- All the companies that have been acquired after 2010, order by the acquisition amount, and retrieve only their
name
andacquisition
field.
Solution
- Query:
{'acquisition.acquired_year': {$gt: 2010}}
- Projection:
{name: 1, acquisition: 1, _id: 0}
- Sort:
{'acquisition.price_amount': -1}
- Order the companies by their
founded year
, retrieving only theirname
andfounded year
.
Solution
- Query:
{'founded_year': { '$ne': null }}
- Projection:
{name: 1, founded_year: 1, _id: 0}
- Sort:
{founded_year: 1}
- All the companies on the 'web'
category
that have more than 4000 employees. Sort them by the number of employees in ascending order.
Solution
- Query:
{$and: [{category_code: 'web'}, {number_of_employees: {$gt: 4000}}]}
- Sort:
{number_of_employees: 1}
- All the companies whose acquisition amount is more than 10,000,000 and the currency is 'EUR'.
Solution
- Query:
{$and: [{'acquisition.price_currency_code': 'EUR'}, {'acquisition.price_amount': {$gt: 10000000}}]}
- All the companies that have been founded between 2000 and 2010, but have not been acquired before 2011.
Solution
- Query:
{$and: [{founded_year: {$gte: 2000}}, {founded_year: {$lte: 2010}}, {'acquisition.acquired_year':{$gt:2011}}]}
Happy Coding! ❤️