-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbTest.js
46 lines (37 loc) · 1.09 KB
/
dbTest.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
const {MongoClient} = require('mongodb');
async function main() {
const uri = "mongodb+srv://jfmcrae:3nTD3WrTyzzQ8wdC@musiclisteningdb.1qmy9w8.mongodb.net/?retryWrites=true&w=majority&appName=MusicListeningDB";
const client = new MongoClient(uri);
try {
await client.connect();
await waitForPing(client);
await findOneAlbum(client);
} catch (e) {
console.error(e);
} finally {
await client.close();
}
}
main().catch(console.error);
async function waitForPing(client) {
await client.db("admin").command({ ping: 1 });
try {
console.log("Ping successful!");
}
catch (e) {
console.log("Ping not successful");
console.error(e)
}
}
async function findOneAlbum(client) {
try {
const database = client.db(ListeningTracker);
const myList = database.collection(JohnListening);
const query = { release: "Vernal Equinox" };
const album = await myList.findOne(query);
console.log(`I found: ${album}`);
}
catch (e) {
console.error(e);
}
}