title: How do I handle command-line arguments in a mongo script?
body: <p>I've been working on some simple scripts to run on mongo from the bash command-line. Originally, I ran them as follows:</p>
  
  <pre><code>$ mongo dbname script.js
  </code></pre>
  
  <p>but I recently came across mikemaccana's answer, <a href="https://stackoverflow.com/a/23909051/2846766">https://stackoverflow.com/a/23909051/2846766</a>, indicating the use of mongo as an interpreter so I can just execute script.js (or any name I choose, with or without the .js) from the command line.</p>
  
  <pre><code>$ script.js
  </code></pre>
  
  <p>I think it's brilliant and clean, but now I'd like to pass in a database name as a command line argument.</p>
  
  <pre><code>$ script.js dbname
  </code></pre>
  
  <p>Here I use the bash-style "$1" to demonstrate what I'm doing in script.js.</p>
  
  <pre><code>#!/usr/bin/env mongo
  var db = new Mongo().getDB($1);
  // Do other things with db, once I resolve the name from the command line.
  </code></pre>
  
  <p>This results in a "ReferenceError: $1 is not defined ...", which is not surprising. But how would I reference command line arguments? Is this going to be a mongo convention? a javascript convention? Is it possible? It would make my command-line experience with mongo much better aesthetically.</p>