1

I have a MongoDB Atlas instance, and am able to connect to it, but I am unable to create an account and access the DB from node.js. I can access the DB from Mongo Shell, after using the mongo command below & > 'use mytestcollection'. Trying to access my testcollection directly from shell return 'Error Authentication Failed'.

Here'e the Shell Connection String..

mongo mongodb://<atlasDB>-1-shard-00-00-<string>.mongodb.net:27017,<atlasDB>-1-shard-00-01-<string>.mongodb.net:27017,<atlasDB>-1-shard-00-02-<string>.mongodb.net:27017/admin?replicaSet=<atlasDB>-1-shard-0 --ssl --username <account>  --password <password>

Here's the Node.js Code

var uri = "mongodb://<atlasDB>-1-shard-00-00-<string>.mongodb.net:27017,"+
                    "<atlasDB>-1-shard-00-01-<string>.mongodb.net:27017,"+
                    "<atlasDB>-1-shard-00-02-<string>.mongodb.net:27017/mytestcollection?"+
                    "replicaSet=-1-shard-0"+
                    "&ssl=true"+
                    "&authSource=<admin>"+
                    "&username=<account>"+
                    "&password=<password>"

and the Node Code is...

MongoDB.connect(uri,function(e,db) { 
        if ( e ) { console.error('Mongo Failed'); return console.log(e); } 
        console.log('MongoSuccess');
        db.collection('mytestcollection').find().toArray(function(e,d) {
             if ( e ) { console.log('error'); console.log(e);  }
             if ( d ) { console.log('test.length',d.length); }
        });
 });

And the error is...

MongoSuccess
error
{ MongoError: not authorized on test to execute command { find: "mytestcollection", filter: {} }
at Function.MongoError.create (/home/ec2-user/frame/node_modules/mongodb-core/lib/error.js:31:11)
at queryCallback (/home/ec2-user/frame/node_modules/mongodb-core/lib/cursor.js:206:36)
at /home/ec2-user/frame/node_modules/mongodb-core/lib/connection/pool.js:430:18
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
name: 'MongoError',
message: 'not authorized on test to execute command { find: "mytestcollection", filter: {} }',
ok: 0,
errmsg: 'not authorized on test to execute command { find: "mytestcollection", filter: {} }',
code: 13 }

Any Suggestions welcome.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user3094755
  • 1,561
  • 16
  • 20

1 Answers1

2

You need to shuffle the MongoDB URI for username and password to authenticate properly. Also you need to specify database name before the replicaSet param, instead of collection name. See Standard Connection String Format for more information on how to format MongoDB URI.

Based on your example:

var uri = "mongodb://[username:password@]<atlasDB>-1-shard-00-00-<string>.mongodb.net:27017,"+
                "<atlasDB>-1-shard-00-01-<string>.mongodb.net:27017,"+
                "<atlasDB>-1-shard-00-02-<string>.mongodb.net:27017/databaseName?"+
                "replicaSet=-1-shard-0"+
                "&ssl=true"+
                "&authSource=<admin>";

MongoClient.connect(uri, function(err, db) {
        if ( e ) { console.error('Connection Failed'); return console.log(e); } 
        console.log("Connected");

        db.collection('collectionName').find().toArray(function(e,d) {
             if ( e ) { console.log('error'); console.log(e);  }
             console.log(d);
        });

        db.close();    
});

The above snippet was tested using MongoDB Node.js driver v2.2

See also:

Wan B.
  • 18,367
  • 4
  • 54
  • 71