MongoDB Atlas User Authentication using node.js
<p>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 &amp; > 'use mytestcollection'.  Trying to access my testcollection directly from shell return 'Error Authentication Failed'.</p>

<p>Here'e the Shell Connection String..</p>

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

<p>Here's the Node.js Code</p>

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

<p>and the Node Code is...</p>

<pre><code>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); }
        });
 });
</code></pre>

<p>And the error is...</p>

<pre><code>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 }
</code></pre>

<p>Any Suggestions welcome.</p>