title: Iterate over all Mongo database
body: <p>I'm relatively new to MongoDB and I've not been able to find a solution for what I'm looking for.</p>
  
  <p>I would like to iterate over all mongo databases and run some command on each collection of each database. I can run the following command to get all db names:</p>
  
  <pre class="lang-js prettyprint-override"><code>db.runCommand( { listDatabases: 1 } ).databases.forEach(function (db) {
      print ("db=" + db.name);
  });
  </code></pre>
  
  <p>But how do I "switch" database within forEach loop so I can run query against each database? I want to use something like <code>use db.name</code> within loop but that's not working.</p>