Skip to content

Instantly share code, notes, and snippets.

@kerminz
Last active May 19, 2019 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerminz/17d386cd0a4c1881872a36686cb4e6f5 to your computer and use it in GitHub Desktop.
Save kerminz/17d386cd0a4c1881872a36686cb4e6f5 to your computer and use it in GitHub Desktop.
Basic MongoDB Connection with NodeJS
const mongodb = require('mongodb')
const MongoClient = mongodb.MongoClient
const connectionURL = 'mongodb://127.0.0.1:27017'
const databaseName = 'customDatabaseName'
MongoClient.connect(connectionURL, { useNewUrlParser: true }, (error, client) => {
if (error) {
return console.log('Unable to connect to database!')
}
const db = client.db(databaseName)
db.collection('users').insertOne({
name: 'Kermin',
age: 32
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment