@@ -6,7 +6,7 @@ async function main(){
6
6
* See https://docs.mongodb.com/ecosystem/drivers/node/ for more details
7
7
*/
8
8
const uri = "mongodb+srv://<username>:<password>@<your-cluster-url>/test?retryWrites=true&w=majority" ;
9
-
9
+
10
10
/**
11
11
* The Mongo Client you will use to interact with your database
12
12
* See https://mongodb.github.io/node-mongodb-native/3.3/api/MongoClient.html for more details
@@ -17,11 +17,8 @@ async function main(){
17
17
// Connect to the MongoDB cluster
18
18
await client . connect ( ) ;
19
19
20
- // Access the listingsAndReviews collection that is stored in the sample_airbnb DB
21
- let collection = client . db ( "sample_airbnb" ) . collection ( "listingsAndReviews" ) ;
22
-
23
20
// Make the appropriate DB calls
24
- await printFiveListings ( collection ) ;
21
+ await listDatabases ( client ) ;
25
22
26
23
} catch ( e ) {
27
24
console . error ( e ) ;
@@ -34,13 +31,12 @@ async function main(){
34
31
main ( ) . catch ( console . err ) ;
35
32
36
33
/**
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
39
36
*/
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 ( ) ;
43
39
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 } ` ) ) ;
46
42
} ;
0 commit comments