diff --git a/starter_code/.ipynb_checkpoints/queries-checkpoint.ipynb b/starter_code/.ipynb_checkpoints/queries-checkpoint.ipynb new file mode 100644 index 0000000..14932fb --- /dev/null +++ b/starter_code/.ipynb_checkpoints/queries-checkpoint.ipynb @@ -0,0 +1,365 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![Ironhack Logo](https://i.imgur.com/1QgrNNw.png)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from pymongo import MongoClient\n", + "client=MongoClient()\n", + "db=client.companies\n", + "companies=db[\"companies\"]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Answers\n", + "### 1. All the companies that it's name match 'Babelgum'. Retrieve only their `name` field." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(filter={\"name\":\"Babelgum\"})" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 2. All the companies that have more than 5000 employees. Limit the search to 20 companies and sort them by **number of employees**." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(filter={\"number_of_employees\":{\"$gt\": 5000 }}, projection={\"number_of_employees\":-1}, limit=10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 3. All the companies founded between 2000 and 2005, both years included. Retrieve only the `name` and `founded_year` fileds." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(filter={ \"$and\": [ {\"founded_year\":{ \"$lte\": 2005}}, {\"founded_year\":{ \"$gte\": 2000}} ] }, projection={\"name\":1,\"founded_year\":1})" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 4. All the companies that had a Valuation Amount of more than 100.000.000 and have been founded before 2010. Retrieve only the `name` and `ipo` fields." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(filter= {\"ipo.valuation_amount\":{\"$gt\":100000000},\"founded_year\":{ \"$lt\": 2010}}, projection={\"ipo\":1, \"name\":1}) " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 5. 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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(filter={\"number_of_employees\":{ \"$lt\": 1000 },\"founded_year\":{ \"$lt\": 2005}}, sort=[(\"number_of_employees\",-1)], limit=10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 6. All the companies that don't include the `partners` field." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(filter={\"partners\": { \"$exists\" : \"false\" } })" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 7. All the companies that have a null type of value on the `category_code` field." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(filter={\"category_code\":\"null\"})" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 8. All the companies that have at least 100 employees but less than 1000. Retrieve only the `name` and `number of employees` fields." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(filter={ \"$and\": [ {\"number_of_employees\":{ \"$lte\": 1000}}, {\"number_of_employees\":{ \"$gte\": 100}}]}, projection={\"name\":1,\"number_of_employees\":1})" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 9. Order all the companies by their IPO price descendently." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(sort=[(\"ipo.valuation_amount\",-1)])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 10. Retrieve the 10 companies with more employees, order by the `number of employees`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(sort=[(\"number_of_employees\",-1)],limit=10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 11. All the companies founded on the second semester of the year. Limit your search to 1000 companies." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(filter={\"founded_month\": {\"$gt\": 6}}, limit=1000)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 12. All the companies that have been 'deadpooled' after the third year." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(filter={\"deadpooled_year\": {\"$gt\": 3}})" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 13. All the companies founded before 2000 that have and acquisition amount of more than 10.000.000" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(filter= {\"founded_year\": {\"$lt\": 2000},'acquisition.price_amount': {\"$gt\": 10000000}})" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 14. All the companies that have been acquired after 2015, order by the acquisition amount, and retrieve only their `name` and `acquisiton` field." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(filter={'acquisition.acquired_year': {\"$gt\": 2015}}, projection={\"name\": 1,\"acquisition\": 1}, sort=[('acquisition.price_amount', -1)])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 15. Order the companies by their `founded year`, retrieving only their `name` and `founded year`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(projection={\"name\": 1,\"founded_year\": 1}, sort=[(\"founded_year\", -1)])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 16. 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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(filter={\"founded_day\": {\"$lte\": 7}}, sort=[( 'acquisition.price_amount', -1)], limit=10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 17. All the companies on the 'web' `category` that have more than 4000 employees. Sort them by the amount of employees in ascendant order." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(filter={\"category_code\": 'web',\"number_of_employees\": {\"$gt\": 4000}}, sort=[(\"number_of_employees\", 1)])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 18. All the companies which their acquisition amount is more than 10.000.000, and currency are 'EUR'.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(filter={'acquisition.price_amount': {\"$gt\": 10000000},'acquisition.price_currency_code': 'EUR'})" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 19. 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` and `acquisition` fields." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(filter= {'acquisition.acquired_month': {\"$lte\": 3}},projection= {\"name\": 1,\"acquisition\": 1}, limit=10)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 20. All the companies that have been founded between 2000 and 2010, but have not been acquired before 2011." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "companies.find(filter= {\"$and\": [{ \"founded_year\": { \"$lte\": 2010 }},{ \"founded_year\": { \"$gte\": 2000 }},{ 'acquisition.acquired_year': { \"$gte\": 2011 }}]})" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/starter_code/queries.ipynb b/starter_code/queries.ipynb index 6eee69f..14932fb 100644 --- a/starter_code/queries.ipynb +++ b/starter_code/queries.ipynb @@ -15,7 +15,8 @@ "source": [ "from pymongo import MongoClient\n", "client=MongoClient()\n", - "db=client.companies" + "db=client.companies\n", + "companies=db[\"companies\"]" ] }, { @@ -31,7 +32,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "companies.find(filter={\"name\":\"Babelgum\"})" + ] }, { "cell_type": "markdown", @@ -45,7 +48,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "companies.find(filter={\"number_of_employees\":{\"$gt\": 5000 }}, projection={\"number_of_employees\":-1}, limit=10)" + ] }, { "cell_type": "markdown", @@ -59,7 +64,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "companies.find(filter={ \"$and\": [ {\"founded_year\":{ \"$lte\": 2005}}, {\"founded_year\":{ \"$gte\": 2000}} ] }, projection={\"name\":1,\"founded_year\":1})" + ] }, { "cell_type": "markdown", @@ -74,7 +81,7 @@ "metadata": {}, "outputs": [], "source": [ - " " + "companies.find(filter= {\"ipo.valuation_amount\":{\"$gt\":100000000},\"founded_year\":{ \"$lt\": 2010}}, projection={\"ipo\":1, \"name\":1}) " ] }, { @@ -89,7 +96,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "companies.find(filter={\"number_of_employees\":{ \"$lt\": 1000 },\"founded_year\":{ \"$lt\": 2005}}, sort=[(\"number_of_employees\",-1)], limit=10)" + ] }, { "cell_type": "markdown", @@ -103,7 +112,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "companies.find(filter={\"partners\": { \"$exists\" : \"false\" } })" + ] }, { "cell_type": "markdown", @@ -117,7 +128,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "companies.find(filter={\"category_code\":\"null\"})" + ] }, { "cell_type": "markdown", @@ -131,7 +144,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "companies.find(filter={ \"$and\": [ {\"number_of_employees\":{ \"$lte\": 1000}}, {\"number_of_employees\":{ \"$gte\": 100}}]}, projection={\"name\":1,\"number_of_employees\":1})" + ] }, { "cell_type": "markdown", @@ -146,7 +161,7 @@ "metadata": {}, "outputs": [], "source": [ - " " + "companies.find(sort=[(\"ipo.valuation_amount\",-1)])" ] }, { @@ -161,7 +176,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "companies.find(sort=[(\"number_of_employees\",-1)],limit=10)" + ] }, { "cell_type": "markdown", @@ -175,7 +192,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "companies.find(filter={\"founded_month\": {\"$gt\": 6}}, limit=1000)" + ] }, { "cell_type": "markdown", @@ -189,7 +208,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "companies.find(filter={\"deadpooled_year\": {\"$gt\": 3}})" + ] }, { "cell_type": "markdown", @@ -203,7 +224,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "companies.find(filter= {\"founded_year\": {\"$lt\": 2000},'acquisition.price_amount': {\"$gt\": 10000000}})" + ] }, { "cell_type": "markdown", @@ -218,7 +241,7 @@ "metadata": {}, "outputs": [], "source": [ - " " + "companies.find(filter={'acquisition.acquired_year': {\"$gt\": 2015}}, projection={\"name\": 1,\"acquisition\": 1}, sort=[('acquisition.price_amount', -1)])" ] }, { @@ -233,7 +256,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "companies.find(projection={\"name\": 1,\"founded_year\": 1}, sort=[(\"founded_year\", -1)])" + ] }, { "cell_type": "markdown", @@ -247,7 +272,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "companies.find(filter={\"founded_day\": {\"$lte\": 7}}, sort=[( 'acquisition.price_amount', -1)], limit=10)" + ] }, { "cell_type": "markdown", @@ -261,7 +288,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "companies.find(filter={\"category_code\": 'web',\"number_of_employees\": {\"$gt\": 4000}}, sort=[(\"number_of_employees\", 1)])" + ] }, { "cell_type": "markdown", @@ -275,7 +304,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "companies.find(filter={'acquisition.price_amount': {\"$gt\": 10000000},'acquisition.price_currency_code': 'EUR'})" + ] }, { "cell_type": "markdown", @@ -289,7 +320,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "companies.find(filter= {'acquisition.acquired_month': {\"$lte\": 3}},projection= {\"name\": 1,\"acquisition\": 1}, limit=10)" + ] }, { "cell_type": "markdown", @@ -303,7 +336,9 @@ "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "companies.find(filter= {\"$and\": [{ \"founded_year\": { \"$lte\": 2010 }},{ \"founded_year\": { \"$gte\": 2000 }},{ 'acquisition.acquired_year': { \"$gte\": 2011 }}]})" + ] } ], "metadata": { @@ -322,7 +357,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.4" + "version": "3.8.8" } }, "nbformat": 4, diff --git a/starter_code/queries.md b/starter_code/queries.md index cb54f8a..c92818c 100644 --- a/starter_code/queries.md +++ b/starter_code/queries.md @@ -3,81 +3,150 @@ # Answers ### 1. All the companies that it's name match 'Babelgum'. Retrieve only their `name` field. - - +-query: {name:"Babelgum"} +-projection: {name:1} +-sort: +-skip: +-limit: ### 2. All the companies that have more than 5000 employees. Limit the search to 20 companies and sort them by **number of employees**. - +-query: {number_of_employees:{ $gt: 5000 }} +-projection: +-sort: {number_of_employees:-1} +-skip: +-limit:20 ### 3. All the companies founded between 2000 and 2005, both years included. Retrieve only the `name` and `founded_year` fileds. - - +-query: { $and: [ {founded_year:{ $lte: 2005}}, {founded_year:{ $gte: 2000}} ] } +-projection: {name:1,founded_year:1} +-sort: +-skip: +-limit: ### 4. All the companies that had a Valuation Amount of more than 100.000.000 and have been founded before 2010. Retrieve only the `name` and `ipo` fields. - - +-query: {"ipo.valuation_amount":{$gt:100000000},founded_year:{ $lt: 2010}} +-projection: {ipo:1, name:1} +-sort: +-skip: +-limit: ### 5. 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. - +-query: {number_of_employees:{ $lt: 1000 },founded_year:{ $lt: 2005}} +-projection: +-sort:{number_of_employees:-1} +-skip: +-limit:10 ### 6. All the companies that don't include the `partners` field. - +-query: {partners: { $exists : false } } aunque tambien vi que puede ser {partners: { $exists : true, $eq:[]} } porque todos incluyen el campo -pero unos lo tienen vacio +-projection: +-sort: +-skip: +-limit: ### 7. All the companies that have a null type of value on the `category_code` field. - - +-query: {category_code:null} +-projection: +-sort: +-skip: +-limit: ### 8. All the companies that have at least 100 employees but less than 1000. Retrieve only the `name` and `number of employees` fields. - +-query: { $and: [ {number_of_employees:{ $lte: 1000}}, {number_of_employees:{ $gte: 100}} ] } +-projection: {name:1,number_of_employees:1} +-sort: +-skip: +-limit: ### 9. Order all the companies by their IPO price descendently. - - +-query: +-projection: +-sort: {"ipo.valuation_amount":-1} +-skip: +-limit: ### 10. Retrieve the 10 companies with more employees, order by the `number of employees` - - +-query: +-projection: +-sort: {number_of_employees:-1} +-skip: +-limit:10 ### 11. All the companies founded on the second semester of the year. Limit your search to 1000 companies. - +-query: {founded_month: {$gt: 6}} +-projection: +-sort: +-skip: +-limit:1000 ### 12. All the companies that have been 'deadpooled' after the third year. - +-query: {deadpooled_year: {$gt: 3}} aunque no estoy segura +-projection: +-sort: +-skip: +-limit: ### 13. All the companies founded before 2000 that have and acquisition amount of more than 10.000.000 - +-query: {founded_year: {$lt: 2000},'acquisition.price_amount': {$gt: 10000000}} +-projection: +-sort: +-skip: +-limit: ### 14. All the companies that have been acquired after 2015, order by the acquisition amount, and retrieve only their `name` and `acquisiton` field. - - +-query: {'acquisition.acquired_year': {$gt: 2015}} +-projection: {name: 1,acquisition: 1} +-sort: {'acquisition.price_amount': -1} +-skip: +-limit: ### 15. Order the companies by their `founded year`, retrieving only their `name` and `founded year`. - - +-query: +-projection: {name: 1,founded_year: 1} +-sort: {founded_year: -1} +-skip: +-limit: ### 16. 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. - - +-query: {founded_day: {$lte: 7}} +-projection: +-sort: {'acquisition.price_amount': -1} +-skip: +-limit:10 ### 17. All the companies on the 'web' `category` that have more than 4000 employees. Sort them by the amount of employees in ascendant order. - +-query: {category_code: 'web',number_of_employees: {$gt: 4000}} +-projection: +-sort: {number_of_employees: 1} +-skip: +-limit: ### 18. All the companies which their acquisition amount is more than 10.000.000, and currency are 'EUR'. - +-query: {'acquisition.price_amount': {$gt: 10000000},'acquisition.price_currency_code': 'EUR'} +-projection: +-sort: +-skip: +-limit: ### 19. 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` and `acquisition` fields. - - +-query: {'acquisition.acquired_month': {$lte: 3}} +-projection: {name: 1,acquisition: 1} +-sort: +-skip: +-limit:10 ### 20. All the companies that have been founded between 2000 and 2010, but have not been acquired before 2011. - - +-query: {$and: [{ founded_year: { $lte: 2010 }},{ founded_year: { $gte: 2000 }},{ 'acquisition.acquired_year': { $gte: 2011 }}]} +-projection: +-sort: +-skip: +-limit: