diff --git a/main.js b/main.js new file mode 100644 index 00000000..5b6962c0 --- /dev/null +++ b/main.js @@ -0,0 +1,271 @@ +const MongoDB = require('mongodb'); +const mongoClient = MongoDB.MongoClient; +const clear = require('clear'); +const readline = require('readline'); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +const url = `mongodb://localhost:27017/crunchbase` + +mongoClient.connect(url, (error, db) => { + if (error) { + console.log('Error trying to connect to the Database'); + console.log(error); + } else { + console.log('Connection established correctly!! 😬'); + + } +}); + +mongoClient.connect(url, (error, db) => { + if (error) { + console.log('Error trying to connect to the Database'); + console.log(error); + } else { + console.log('Connection established correctly!! 😬'); + + function mainMenu(){ + clear(); + printMenu(); + rl.question('Type an option: ', (option) => { + switch(option){ + case "1": + db.collection('companies').find({}, {name: 1, _id: 0}).toArray((error, result) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(result); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "2": + db.collection('companies').find({}, {name: 1, _id: 0}).count((error, result) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(result); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "3": + db.collection('companies').find({founded_year: 2004}).count((error, counter) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(counter); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "4": + db.collection('companies').find({founded_year: 2004,founded_month:2}, {name: 1, _id: 0}).toArray((error, result) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(result); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "5": + db.collection('companies').find({founded_year: 2004,$and: [{ founded_month: { $gte: 4 } }, { founded_month: { $lte: 6 } }] },{name: 1, _id: 0}).toArray((error, counter) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(counter); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "6": + db.collection('companies').find({"offices.city": { $in: ["Barcelona"] }}, {name: 1, _id: 0}).toArray((error, result) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(result); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "7": + db.collection('companies').find({},{name: 1,number_of_employees: 1, _id: 0}).sort({number_of_employees: -1}).limit(10).toArray((error, result) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(result); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "8": + db.collection('companies').find({name:"Facebook"}).toArray((error, result) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(result); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "9": + db.collection('companies').find({name:"Facebook"},{number_of_employees: 1, _id: 0}).toArray((error, result) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(result); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "10": + db.collection('companies').find({name: "Facebook"}, {name: 1,"products": 1, _id: 0}).toArray((error, result) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(result); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "11": + db.collection('companies').find({name: "Facebook","relationships.is_past": false}, {"relationships.person.last_name": 1, _id: 0}).toArray((error, result) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(result); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "12": + db.collection('companies').find({name: "Facebook","relationships.is_past": true}).count((error, result) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(result); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "13": + db.collection('companies').find({"relationships.person.permalink":"david-ebersman" }, {name: 1, _id: 0}).toArray((error, result) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(result); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "14": + db.collection('companies').find({name: "Facebook"}, {"competitions.competitor.name":1,_id:0}).toArray((error, result) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(result); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "15": + db.collection('companies').find({tag_list:/social-network/},{name: 1,_id: 0}).toArray((error, result) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(result); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "16": + db.collection('companies').find({tag_list:/social-network/, $and: [{ founded_year: { $gte: 2002 } }, { founded_year: { $lte: 2016 } }]}).count((error, result) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(result); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "17": + db.collection('companies').find({"offices.city": { $in: ["London"] }}, {name: 1, _id: 0}).toArray((error, result) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(result); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "18": + db.collection('companies').find({tag_list:/social-network/, $and: [{ founded_year: { $gte: 2002 } }, { founded_year: { $lte: 2016 } }],"offices.city": { $in: ["New York"] }}).count((error, result) => { + if (error) { + console.log(error); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } else { + console.log(result); + rl.question(`\nType enter to continue: `, (answer) => { mainMenu() }); + } + }) + break; + case "0": + console.log(`👋👋👋👋 😞 \n`); + db.close((error) => { process.exit(0) }); + break; + default: + mainMenu(); + break; + } + }); + } + + mainMenu(); + + } +}); + +function printMenu(){ + console.log(` +0.- Exit +1.- List by name all companies. +2.- How many companies are there? +3.- How many companies were founded in 2004? +4.- List by name all companies founded in february of 2004. +5.- List by name all companies founded in the summer of 2004 (april to june) sorted by date. +6.- What companies have offices in "Barcelona". +7.- List the 10 companies with more employees sorted ascending (show name and employees). +8.- Find the company with the name "Facebook" +9.- How many employees has Facebook? +10.- List the name of all the products of Facebook +11.- List the people that are working at Facebook right now (check relationships field) +12.- How many people are not working anymore at Facebook +13.- List all the companies where "david-ebersman" has worked. +14.- List by name the competitors of Facebook +15.- Names of the companies that has "social-networking" in tag-list (be aware that the value of field is a string check regex operators) +16.- How many companies that has "social-network" in tag-list and founded between 2002 and 2016 inclusive +17.- Names and locations of companies that have offices in London +18.- How many companies that has "social-network" in tag-list and founded between 2002 and 2016 inclusive and has offices in New York +`); +} + diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 00000000..d6397d6d --- /dev/null +++ b/package-lock.json @@ -0,0 +1,118 @@ +{ + "name": "lab-mongo-crunchbase", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "bson": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/bson/-/bson-1.0.9.tgz", + "integrity": "sha512-IQX9/h7WdMBIW/q/++tGd+emQr0XMdeZ6icnT/74Xk9fnabWn+gZgpE+9V+gujL3hhJOoNrnDVY7tWdzc7NUTg==" + }, + "buffer-shims": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/buffer-shims/-/buffer-shims-1.0.0.tgz", + "integrity": "sha1-mXjOMXOIxkmth5MCjDR37wRKi1E=" + }, + "clear": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/clear/-/clear-0.1.0.tgz", + "integrity": "sha512-qMjRnoL+JDPJHeLePZJuao6+8orzHMGP04A8CdwCNsKhRbOnKRjefxONR7bwILT3MHecxKBjHkKL/tkZ8r4Uzw==" + }, + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" + }, + "es6-promise": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-3.2.1.tgz", + "integrity": "sha1-7FYjOGgDKQkgcXDDlEjiREndH8Q=" + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" + }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, + "mongodb": { + "version": "2.2.36", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-2.2.36.tgz", + "integrity": "sha512-P2SBLQ8Z0PVx71ngoXwo12+FiSfbNfGOClAao03/bant5DgLNkOPAck5IaJcEk4gKlQhDEURzfR3xuBG1/B+IA==", + "requires": { + "es6-promise": "3.2.1", + "mongodb-core": "2.1.20", + "readable-stream": "2.2.7" + } + }, + "mongodb-core": { + "version": "2.1.20", + "resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-2.1.20.tgz", + "integrity": "sha512-IN57CX5/Q1bhDq6ShAR6gIv4koFsZP7L8WOK1S0lR0pVDQaScffSMV5jxubLsmZ7J+UdqmykKw4r9hG3XQEGgQ==", + "requires": { + "bson": "~1.0.4", + "require_optional": "~1.0.0" + } + }, + "process-nextick-args": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-1.0.7.tgz", + "integrity": "sha1-FQ4gt1ZZCtP5EJPyWk8q2L/zC6M=" + }, + "readable-stream": { + "version": "2.2.7", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.2.7.tgz", + "integrity": "sha1-BwV6y+JGeyIELTb5jFrVBwVOlbE=", + "requires": { + "buffer-shims": "~1.0.0", + "core-util-is": "~1.0.0", + "inherits": "~2.0.1", + "isarray": "~1.0.0", + "process-nextick-args": "~1.0.6", + "string_decoder": "~1.0.0", + "util-deprecate": "~1.0.1" + } + }, + "require_optional": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz", + "integrity": "sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==", + "requires": { + "resolve-from": "^2.0.0", + "semver": "^5.1.0" + } + }, + "resolve-from": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-2.0.0.tgz", + "integrity": "sha1-lICrIOlP+h2egKgEx+oUdhGWa1c=" + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "semver": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", + "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==" + }, + "string_decoder": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", + "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", + "requires": { + "safe-buffer": "~5.1.0" + } + }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 00000000..d22a1535 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "lab-mongo-crunchbase", + "version": "1.0.0", + "description": "![Ironhack Logo](https://i.imgur.com/1QgrNNw.png)", + "main": "main.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/Cylabeth/lab-mongo-crunchbase.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/Cylabeth/lab-mongo-crunchbase/issues" + }, + "homepage": "https://github.com/Cylabeth/lab-mongo-crunchbase#readme", + "dependencies": { + "clear": "^0.1.0", + "mongodb": "^2.2.36" + } +}