It's very important to understand that you cannot use shell command helpers from an external JavaScript script, even if you run the script from inside the shell! Many of the shell methods you may wish to use reference the db object. When you are running shell methods from inside the shell, this is not a problem: when you issue the use command, this anchors the db object to the database currently in use. Since the use command is a shell command helper, however, it cannot be used inside an external script. Accordingly, the first set of commands you need to add to an external shell script is needed to set the db object to the desired database.
This can be accomplished as follows, where <DATABASE> is the name of the target database you wish to operate upon:
conn = new Mongo();
db = conn.getDB("<DATABASE>");
From this point on, you can use the db object as you would with any other shell method. Here is a sample shell script that assigns the db...