Skip to content

Instantly share code, notes, and snippets.

@mathiasgheno
Last active January 22, 2018 22:44
Show Gist options
  • Save mathiasgheno/9e6b58275758a583703aaba417ee1d4b to your computer and use it in GitHub Desktop.
Save mathiasgheno/9e6b58275758a583703aaba417ee1d4b to your computer and use it in GitHub Desktop.
MongoDB Auth Node.js Snippet
const mongodb = require("mongodb");
const usuario = 'myUserAdmin';
const senha = 'abc123@';
const url = `mongodb://localhost:27017/admin`;
mongodb
.MongoClient
.connect(url)
.then((db) => {
db.authenticate(usuario, senha)
.then(() => {
db.db('teste')
.collection('admin')
.insertOne({funcionando: true})
.then((resultadoOperacao) => {
console.log(resultadoOperacao.insertedId);
});
});
})
.catch(reason => console.log('Erro ao conectar: ', reason));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment