Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4a8664f

Browse files
committed
Update connection.js to list databases
1 parent 38a95c2 commit 4a8664f

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

‎connection.js‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ async function main(){
66
* See https://docs.mongodb.com/ecosystem/drivers/node/ for more details
77
*/
88
const uri = "mongodb+srv://<username>:<password>@<your-cluster-url>/test?retryWrites=true&w=majority";
9-
9+
1010
/**
1111
* The Mongo Client you will use to interact with your database
1212
* See https://mongodb.github.io/node-mongodb-native/3.3/api/MongoClient.html for more details
@@ -17,11 +17,8 @@ async function main(){
1717
// Connect to the MongoDB cluster
1818
await client.connect();
1919

20-
// Access the listingsAndReviews collection that is stored in the sample_airbnb DB
21-
let collection = client.db("sample_airbnb").collection("listingsAndReviews");
22-
2320
// Make the appropriate DB calls
24-
await printFiveListings(collection);
21+
await listDatabases(client);
2522

2623
} catch (e) {
2724
console.error(e);
@@ -34,13 +31,12 @@ async function main(){
3431
main().catch(console.err);
3532

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

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /