-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlisting-8.4.js
80 lines (69 loc) · 2.19 KB
/
listing-8.4.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"use strict";
const MongoClient = require('mongodb').MongoClient;
const hostName = "mongodb://127.0.0.1:7000";
const databaseName = "weather_stations";
const collectionName = "daily_readings";
//
// Open the connection to the database.
//
function openDatabase () {
return MongoClient.connect(hostName)
.then(client => {
const db = client.db(databaseName);
const collection = db.collection(collectionName);
return {
collection: collection,
close: () => {
return client.close();
},
};
});
};
let numRecords = 0;
let numWindows = 0;
//
// Read a single data window from the database.
//
function readWindow (collection, windowIndex, windowSize) {
const skipAmount = windowIndex * windowSize;
const limitAmount = windowSize;
return collection.find()
.skip(skipAmount)
.limit(windowSize)
.toArray();
};
//
// Read the entire database, window by window.
//
function readDatabase (collection, startWindowIndex, windowSize) {
return readWindow(collection, startWindowIndex, windowSize)
.then(data => {
if (data.length > 0) {
// We got some data back.
console.log("Window with " + data.length + " elements.");
// TODO: Add your data processsing here.
numRecords += data.length;
++numWindows;
// Read the entire database using an asynchronous recursive traversal.
return readDatabase(collection, startWindowIndex+1, windowSize);
}
else {
// We retreived no data, finished reading.
}
})
};
openDatabase()
.then(db => {
const windowSize = 100;
return readDatabase(db.collection, 0, windowSize)
.then(() => {
return db.close(); // Close database when done.
});
})
.then(() => {
console.log("Processed " + numRecords + " records in " + numWindows + " windows.");
})
.catch(err => {
console.error("An error occurred reading the database.");
console.error(err);
});