30

I'm using native mongo driver in Joyent cloud, the node.js application runs fine locally but in Joyent when I run with the username/password that they provided it fails to connect.

This is the code I use to connect:

var db = new MongoDB(dbName, new Server('localhost', 27017 , {auto_reconnect: true}), {w: 1});
db.open(function(e, db){
if (e) {
    console.log(e);
} else{
    console.log('connected to database :: ' + dbName);
    //db.admin().authenticate('admin', '+(uihghjk', function(de , db){
    // if(e){
    //     console.log("could not authenticate");
    // }else {
    //console.log('connected to database :: ' + dbName);
    // }
    // });
}
});

What's stopping me from connecting successfully?

Nick is tired
  • 6,860
  • 20
  • 39
  • 51
santosh
  • 301
  • 1
  • 3
  • 5
  • The above commented code works fine for authentication. The problem was they mixed up with credentials provided for mongodb, to verify login and password, ssh to joyent and enter $(mdata-get mongodb_pw), verify the given pswd works in "mongo -uadmin -p$(mdata-get mongodb_pw) admin" – santosh Apr 21 '13 at 03:16

4 Answers4

47

Easier if you just use MongoClient

MongoClient.connect('mongodb://admin:password@localhost:27017/db', function (err, db) {
Jonathan Ong
  • 19,927
  • 17
  • 79
  • 118
  • This is very correct, but how can I hide the credentials from the repository? is there a way to use a hash key or something like that? – Mavro Aug 08 '21 at 04:22
  • 1
    @Mavro in one project with mongoose I see the whole path with credentials stored in a config file which is not committed to repository, then variable used in connect. – Vladislav Povorozniuc Nov 24 '21 at 18:06
9

the working code would be like this

const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://<username>:<password>@cluster0-saugt.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
    // creating collection
    const collection = client.db("test").collection("devices");
    // perform actions on the collection object
    client.close();
});
Jamil Noyda
  • 3,321
  • 2
  • 21
  • 26
  • In order to leverage the DNS seed list, use a connection string prefix of `mongodb+srv` [as explained](https://www.mongodb.com/docs/manual/reference/connection-string/#dns-seed-list-connection-format) – Timo Aug 19 '22 at 16:28
6

Standard URI connection scheme is:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

For Example:

mongodb://admin:pass@abc.com/

Reference: https://docs.mongodb.com/manual/reference/connection-string/

Yug Singh
  • 3,112
  • 5
  • 27
  • 52
VAIBHAV GOUR
  • 167
  • 1
  • 3
2

Use <username> : <password> right after // in the URL.

I am using Mongoose to connect and my username and password is admin admin

mongodb+srv://admin:admin@cluster0.c1xrp

This worked for me.

cigien
  • 57,834
  • 11
  • 73
  • 112
  • The only thing this "new" answer does is to repeat what half of the other answers are already saying, including [another well-received answer](https://stackoverflow.com/a/52288654/14267427). – Tyler2P Dec 24 '21 at 19:38