1 | 'use strict'; |
||
2 | const {MongoClient} = require('mongodb'); |
||
3 | |||
4 | MongoClient.connect('mongodb://localhost:27017/JokesApp', (err, db) => { |
||
5 | if (err) { |
||
6 | return console.log('error connecting to mongodb server'); |
||
0 ignored issues
–
show
Debugging Code
introduced
by
![]() |
|||
7 | } |
||
8 | |||
9 | console.log('connected to mongodb server'); |
||
10 | |||
11 | db.collection('Jokes').findOneAndUpdate({ |
||
12 | joke : 'Sample joke' |
||
13 | }, |
||
14 | { |
||
15 | $set : { |
||
16 | joke : 'This was updated joke', |
||
17 | likes : 1 |
||
18 | } |
||
19 | }, { |
||
20 | returnOriginal : true |
||
21 | }).then((updatedDoc) => { |
||
22 | console.log(updatedDoc); |
||
0 ignored issues
–
show
|
|||
23 | }, (err) => { |
||
24 | console.log('Error updating. E: ', err); |
||
0 ignored issues
–
show
|
|||
25 | }); |
||
26 | |||
27 | // display all the jokes after updating |
||
28 | db.collection('Jokes').find().toArray().then((docs) => { |
||
29 | console.log(docs); |
||
0 ignored issues
–
show
|
|||
30 | }, (err) => { |
||
31 | console.log('error displaying all jokes. E: ', err); |
||
0 ignored issues
–
show
|
|||
32 | }); |
||
0 ignored issues
–
show
|
|||
33 | //db.close(); |
||
34 | }); |