title: MongoError: listCollections failed
body: <p>I have setted up a mongo database, which works with authentication. </p>
  
  <p>I'm trying to copy a database using <a href="https://github.com/mongodb/node-mongodb-native" rel="nofollow noreferrer">mongodb native driver</a>.</p>
  
  <hr>
  
  <p>The problem is my command get rejected as :</p>
  
  <pre><code>MongoError: listCollections failed: { ok: 0.0, errmsg: "not authorized on SESSION to execute command { listCollections: 1, filter: { $or: [ { type: "collection" }, { type: { $exists: false } } ] }, cu...", code: 13, codeName: "Unauthorized" }
  </code></pre>
  
  <hr>
  
  <p>Here is how I run my database :</p>
  
  <pre><code>/usr/bin/mongod --quiet
                  --setParameter authenticationMechanisms=SCRAM-SHA-1
                  --auth
                  --port 27017
                  --dbpath /database
  </code></pre>
  
  <hr>
  
  <p>Here is the user I'm using to connect</p>
  
  <pre><code>  db.createUser({
        "user": "USER",
        "pwd": "PASS",
        "roles": [ "root" ]
    });
  </code></pre>
  
  <p>I also tried using :</p>
  
  <pre><code>  db.createUser({
        "user": "USER",
        "pwd": "PASS",
        "roles": [ { "role": "root", "db": "admin" } ]
    });
  </code></pre>
  
  <hr>
  
  <p>Here is how I connect (the connection succeed) :</p>
  
  <pre><code>const url = "mongodb://USER:PASS@172.42.0.2:27017/SESSION?authSource=admin";
  
  MongoClient.connect(url, ...
  </code></pre>
  
  <hr>
  
  <p>Here is how I request the copy :</p>
  
  <pre><code>const mongoCommand = {
       copydb: 1,
       fromhost: "172.42.0.2",
       fromdb: "SESSION",
       todb: "SESSION_COPY",
  };
  
  // Perform the copy
  this.db.admin().command(mongoCommand, function (err, data) {
  </code></pre>
  
  <hr>
  
  <p>I am using :</p>
  
  <pre><code>Mongodb (database) v3.4.7
  Mongodb (node package) v2.2.31
  </code></pre>
  
  <hr>
  
  <p>Thanks :)</p>
  
  <hr>
  
  <p>Some posts that get me through this :</p>
  
  <p><a href="https://stackoverflow.com/questions/22067612/mongodb-not-authorized-for-query-admin-system-users">mongodb-not-authorized-for-query-admin-system-users</a></p>
  
  <p><a href="https://stackoverflow.com/questions/36403749/how-can-i-execute-db-copydatabase-through-nodejss-mongodb-native-driver">how-can-i-execute-db-copydatabase-through-nodejss-mongodb-native-driver</a></p>