1+ // Count documents in a collection
2+
13import { MongoClient } from "mongodb" ;
24
3- // Replace the uri string with your MongoDB deployment's connection string.
5+ // Replace the uri string with your MongoDB deployment's connection string
46const uri = "<connection string uri>" ;
57
68const client = new MongoClient ( uri ) ;
@@ -10,21 +12,20 @@ async function run() {
1012 const database = client . db ( "sample_mflix" ) ;
1113 const movies = database . collection ( "movies" ) ;
1214
13- // Estimate the total number of documents in the collection
14- // and print out the count.
15+ /* Print the estimate of the number of documents in the
16+ "movies" collection */
1517 const estimate = await movies . estimatedDocumentCount ( ) ;
1618 console . log ( `Estimated number of documents in the movies collection: ${ estimate } ` ) ;
1719
18- // Query for movies from Canada.
20+ /* Print the number of documents in the "movies" collection that
21+ match the specified query */
1922 const query = { countries : "Canada" } ;
20-
21- // Find the number of documents that match the specified
22- // query, (i.e. with "Canada" as a value in the "countries" field)
23- // and print out the count.
2423 const countCanada = await movies . countDocuments ( query ) ;
2524 console . log ( `Number of movies from Canada: ${ countCanada } ` ) ;
2625 } finally {
26+ // Close the connection after the operations complete
2727 await client . close ( ) ;
2828 }
2929}
30+ // Run the program and print any thrown exceptions
3031run ( ) . catch ( console . dir ) ;
0 commit comments