Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WEB-MAD- DE] Ana Bermúdez #231

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
266 changes: 266 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
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!! 😬');



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').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, 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 "4":
db.collection('companies').find({ $and: [{ 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({ $and: [{ founded_year: 2004 },{ founded_month: {$gte: 2}}, { founded_month : {$lte: 6}}]}, {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 "6":
db.collection('companies').find({"offices.city": "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"}, {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 "9":
db.collection('companies').find({name: "Facebook"}, {name:1, 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":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solo te faltó acceder a la posición 0 del array resultante, tal que así:

case '10':
            db.collection('companies')
              .find(
                { name: 'Facebook' },
                { name: 1, 'products.name': 1, _id: 0 }
              )
              .toArray((error, result) => {
                if (error) {
                  console.log(error);
                  rl.question(`\nType enter to continue: `, (answer) => {
                    mainMenu();
                  });
                } else {
                  console.log(result[0]); <===================================
                  rl.question(`\nType enter to continue: `, (answer) => {
                    mainMenu();
                  });
                }
              });
            break;

db.collection('companies').find({name: "Facebook"}, {name: 1, "products.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 "11":
Copy link
Contributor

@ta-web-mad ta-web-mad Sep 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Primero en el find debías simplemente hacer el find a la base de datos y traer los que habían tenido trayectoria en la empresa.

db.collection('companies').find({"name": "Facebook"}, {relationships: 1, _id: 0})

En el resultado de esa query, filtrabas por los que trabajan ahora mismo, manejando la propiedad is_past

else {
        result[0].relationships.filter(value => {
       if(!value.is_past)console.log(`Worker: ${value.person.first_name} ${value.person.last_name}`);
});

db.collection('companies').find({name: "Facebook", "relationships.is_past" : { $exists: false }}, {"relationships.person": 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":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Esta es exactamente igual que la anterior, aunque tocaba modificar el filter al resultado de la query para que esta vez solo dejara los que ya no trabajan, te falló el planteamiento inicial, te dejo la query corregida:

case "12":
              db.collection('companies').find({"name": "Facebook"}, {relationships: 1, _id: 0}).toArray((error, result) => {
                if (error) {
                  console.log(error);
                  rl.question(`\nType enter to continue: `, (answer) => { mainMenu() });
                } else {
                  result[0].relationships.filter(value => {
                    if(value.is_past)console.log(`ExWorker: ${value.person.first_name} ${value.person.last_name}`);
                  });
                  rl.question(`\nType enter to continue: `, (answer) => { mainMenu() });
                }
              })
            break;

db.collection('companies').find({name: "Facebook"}, {"relationships.is_past" : { $exists: true }},{"product.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 "13":
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

En esta debías indicar en la query dentro del objeto person, nombre y apellido por separado:

db.collection('companies').find({$and: [
                {"relationships.person.first_name": "David"},
                {"relationships.person.last_name": "Ebersman"}
              ]}, {name: 1, _id: 0})

db.collection('companies').find( {"relationships.person": "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-networking"}, {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({$and : [{tag_list: "social-networking"},{ founded_year: {$gte: 2002}}, { founded_year: {$lte: 2016}}]}, {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 "17":
db.collection('companies').find({"offices.city": "London"}, {name: 1, "offices.address1": 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({$and : [{"offices.city": "New York"},{tag_list: "social-networking"},{ founded_year: {$gte: 2002}}, { founded_year: {$lte: 2016}}]}, {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 "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
`);
}

118 changes: 118 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading