Skip to content

Commit

Permalink
Update connection.js to list databases
Browse files Browse the repository at this point in the history
  • Loading branch information
ljhaywar committed Oct 11, 2019
1 parent 38a95c2 commit 4a8664f
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ async function main(){
* See https://docs.mongodb.com/ecosystem/drivers/node/ for more details
*/
const uri = "mongodb+srv://<username>:<password>@<your-cluster-url>/test?retryWrites=true&w=majority";

/**
* The Mongo Client you will use to interact with your database
* See https://mongodb.github.io/node-mongodb-native/3.3/api/MongoClient.html for more details
Expand All @@ -17,11 +17,8 @@ async function main(){
// Connect to the MongoDB cluster
await client.connect();

// Access the listingsAndReviews collection that is stored in the sample_airbnb DB
let collection = client.db("sample_airbnb").collection("listingsAndReviews");

// Make the appropriate DB calls
await printFiveListings(collection);
await listDatabases(client);

} catch (e) {
console.error(e);
Expand All @@ -34,13 +31,12 @@ async function main(){
main().catch(console.err);

/**
* Print the names of five Airbnb listings
* @param {Collection} collection The collection to search
* Print the names of all available databases
* @param {MongoClient} client A MongoClient that is connected to a cluster with the sample_airbnb database
*/
async function printFiveListings(collection){
let cursor = await collection.find({}).limit(5);
let docs = await cursor.toArray();
async function listDatabases(client){
databases = await client.db("sample_airbnb").admin().listDatabases();

console.log("Found Airbnb listings in the database:");
docs.forEach(doc => console.log(` - ${doc.name}`));
console.log("Databases:");
databases.databases.forEach(db => console.log(` - ${db.name}`));
};

0 comments on commit 4a8664f

Please sign in to comment.