-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexSolved.js
66 lines (46 loc) · 1.64 KB
/
indexSolved.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Import the Mongo Client for connection
const {MongoClient} = require('mongodb');
async function listDatabases(client) {
let databasesList = await client.db().admin().listDatabases();
console.log("Databases:");
// this just iterates over the result to print it
databasesList.databases.forEach(db => console.log('- ' + db.name));
}
// Finds one restaurant that has this name
async function findRestaurantByName(client, name) {
let result = await client.db("sample_restaurants").collection("restaurants")
.findOne({ name: name });
console.log(result);
}
// Finds all restaurants that have the given cuisine name
async function findAllRestaurantsWithCuisine(client, cuisine) {
let result = await client.db("sample_restaurants").collection("restaurants").find({ cuisine: cuisine });
console.log(result);
}
// Creates a new restaurant for the database
async function createNewRestaurant(client, newRestaurant) {
const result = await client.db("sample_restaurants").collection("restaurants").insertOne(newRestaurant);
}
async function main() {
const uri = "YOUR_DATABASE_STRING"
const client = new MongoClient(uri);
try {
await client.connect();
// Add code here you want to do with the database
let name = "Happy Garden";
await findRestaurantByName(client, name);
let cuisine = "Delicatessen";
await findAllRestaurantsWithCuisine(client, cuisine);
let newRestaurant = {
name: "Chicken Lou's",
cuisine: "Tasty",
borough: "Northeastern"
}
await createNewRestaurant(client, newRestaurant);
} catch (e) {
console.error(e);
} finally {
await client.close();
}
}
main();