new Mongo() new Mongo(<host>) new Mongo(<host:port>)
Consider the following example that instantiates a new connection to the MongoDB instance running on localhost on the default port and sets the global db variable to myDatabase using the getDB() method:
conn = new Mongo(); db = conn.getDB("myDatabase");
Additionally, you can use the connect() method to connect to the MongoDB instance. The following example connects to the MongoDB instance that is running on localhost with the non-default port 27020 and set the global db variable:
db = connect("localhost:27020/myDatabase");
mongo test --eval "printjson(db.getCollectionNames())"
mongo localhost:27017/test myjsfile.js