I'm using the MongoDB shell and like to define some shortcuts. For example, it would be nice to abbreviate show databases
with sd
.
I already managed to add a function hw()
to the MongoDB shell by adding it's definition to the ~/.mongorc.js
:
function hw() {
print("Hello World.");
}
When I type hw()
in the mongo-shell, it prints out Hello World.
Question 1: Is it also possible to execute the function without having to type the brackets (i.e. hw
instead of hw()
)?
I tried to bind the function to variable using anonymous functions, but still I have to type brackets, otherwise the definition of the function is printed out
hw=function(){ print("Hello World (anonymous)."); };
Question 2: How can I execute MongoDB commands from within my functions? I tried:
function sd() {
show databases;
}
but that gives an error during startup of the MongoDB shell:
SyntaxError: Unexpected identifier at /home/edward/.mongorc.js:2