-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
listing-8.7.js
44 lines (40 loc) · 1.12 KB
/
listing-8.7.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
"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();
},
};
});
};
openDatabase()
.then(db => {
return db.collection.find() // Retreive only specified fields.
.sort({
Year: 1
})
.toArray()
.then(data => {
console.log(data);
})
.then(() => db.close()); // Close database when done.
})
.then(() => {
console.log("Done.");
})
.catch(err => {
console.error("An error occurred reading the database.");
console.error(err);
});