Last active
January 30, 2020 02:22
-
-
Save iamdylanngo/f7272c2e1b2864cf39b9de429c8b9b09 to your computer and use it in GitHub Desktop.
nodejs-mongodb
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
# default | |
var MongoClient = require('mongodb').MongoClient; | |
// Connection URL | |
var url = 'mongodb://admin:123456@localhost:27017?authMechanism=DEFAULT'; | |
// Use connect method to connect to the Server | |
MongoClient.connect(url, { useUnifiedTopology: true }, function (err, db) { | |
if (err) throw err; | |
var dbo = db.db("mydb"); | |
dbo.createCollection("customers", function (err, res) { | |
if (err) throw err; | |
console.log("Collection created!"); | |
db.close(); | |
}); | |
}); | |
# SCRAM-SHA-1 | |
var MongoClient = require('mongodb').MongoClient, | |
f = require('util').format, | |
assert = require('assert'); | |
// Connection URL | |
var url = 'mongodb://dave:password@localhost:27017?authMechanism=SCRAM-SHA-1&authSource=db'; | |
// Use connect method to connect to the Server | |
MongoClient.connect(url, function(err, db) { | |
assert.equal(null, err); | |
console.log("Connected correctly to server"); | |
db.close(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
docker run -d --name mongo -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=123456 -p 27017:27017 mongo