Created
August 3, 2019 20:31
-
-
Save ShawonAshraf/0a8ecf1678b98a70baa4196dfd6394df to your computer and use it in GitHub Desktop.
Code for mongodb-nodejs-medium article
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const mongodb = require('mongodb'); | |
| const data = require('./data.json'); | |
| const games = data.games; // that's what we need actually | |
| const url = 'mongodb://localhost:27017/learnMongo'; | |
| const dbName = 'learnMongo'; // let's say that's our database name | |
| // not needed however | |
| // use the new url parser to avoid warning messages | |
| const client = new mongodb.MongoClient(url, { useNewUrlParser: true }); | |
| // I'm using asyn await and try catch | |
| // because I hate promise and callback hell .__. | |
| const app = async() => { | |
| try { | |
| await client.connect(); | |
| console.log('Connected to the database server!'); | |
| console.log('Closing connection ...'); | |
| client.close(); | |
| } catch (err) { | |
| // console.log('Error connecting to the database.'); | |
| console.log(err.toString()); | |
| } | |
| }; | |
| // envoke app | |
| app(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment