Skip to content

Instantly share code, notes, and snippets.

@ShawonAshraf
Created August 3, 2019 20:31
Show Gist options
  • Select an option

  • Save ShawonAshraf/0a8ecf1678b98a70baa4196dfd6394df to your computer and use it in GitHub Desktop.

Select an option

Save ShawonAshraf/0a8ecf1678b98a70baa4196dfd6394df to your computer and use it in GitHub Desktop.
Code for mongodb-nodejs-medium article
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