File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ var numRecords = 0 ;
4+
5+ //
6+ // Read the entire database, document by document using a database cursor.
7+ //
8+ var readDatabase = cursor => {
9+ return cursor . next ( )
10+ . then ( record => {
11+ if ( record ) {
12+ // Found another record.
13+ console . log ( record ) ;
14+ ++ numRecords ;
15+
16+ // Read the entire database using an asynchronous recursive traversal.
17+ return readDatabase ( cursor ) ;
18+ }
19+ else {
20+ // No more records.
21+ }
22+ } ) ;
23+ } ;
24+
25+ var MongoClient = require ( 'mongodb' ) . MongoClient ;
26+ MongoClient . connect ( 'mongodb://localhost' )
27+ . then ( client => {
28+ var db = client . db ( 'weather_stations' ) ;
29+ var collection = db . collection ( 'daily_readings' ) ;
30+ return readDatabase ( collection . find ( ) ) // NOTE: You could use a query here.
31+ . then ( ( ) => client . close ( ) ) ; // Close database when done.
32+ } )
33+ . then ( ( ) => {
34+ console . log ( "Displayed " + numRecords + " records." ) ;
35+ } )
36+ . catch ( err => {
37+ console . error ( "An error occurred reading the database." ) ;
38+ console . error ( err ) ;
39+ } ) ;
You can’t perform that action at this time.
0 commit comments