Total Complexity | 11 |
Complexity/F | 1.57 |
Lines of Code | 40 |
Function Count | 7 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | // import MongoClient from the mongodb package |
||
2 | const {MongoClient} = require('mongodb'); |
||
3 | |||
4 | // connect to mongodb server |
||
5 | MongoClient.connect('mongodb://localhost:27017/JokesApp', (err, db) => { |
||
6 | if (err) { |
||
7 | return console.log('Failed to connect to mongoDB server : ' + err); |
||
|
|||
8 | } |
||
9 | |||
10 | console.log('Connected to MongoDB Server'); |
||
11 | |||
12 | //get the count of jokes stored |
||
13 | db.collection('Jokes').count().then((count) => { |
||
14 | console.log(`Jokes count = ${count}\n`); |
||
15 | }, (err) => { |
||
16 | if (err) { |
||
17 | return console.log('error getting count', err); |
||
18 | } |
||
19 | }); |
||
20 | |||
21 | // fetch all the jokes/documets with 10 likes |
||
22 | db.collection('Jokes').find({likes : 10}).toArray().then((docs) => { |
||
23 | console.log(JSON.stringify(docs, undefined, 2)); |
||
24 | }, (err)=> { |
||
25 | if (err) { |
||
26 | return console.log('Fetch error : ', err); |
||
27 | } |
||
28 | }); |
||
29 | |||
30 | //fetch all the users from the db |
||
31 | db.collection('Users').find().toArray().then((docs) => { |
||
32 | // print all the users |
||
33 | console.log(JSON.stringify(docs, undefined, 2)); |
||
34 | }, (err) => { |
||
35 | if(err) { |
||
36 | return console.log('Error Fetching users : ', err); |
||
37 | } |
||
38 | }); |
||
39 | |||
40 | //db.close(); |
||
41 | }); |