Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define aliases in the MongoDB Shell?

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

like image 410
Edward Avatar asked Jan 22 '15 16:01

Edward


People also ask

What is MongoDB alias?

In MongoDB, since the database is schemaless and the field names are included for every document in the JSON, much more storage space is required than traditional databases with schemas. Because of this, a common practice is to store very short field names in the documents to conserve space.

What is NumberLong in MongoDB?

By default, the mongo shell treats all numbers as floating-point values. The mongo shell provides the NumberLong() class to handle 64-bit integers. The NumberLong() constructor accepts the long as a string: NumberLong("2090845886852")

What is Int32 in MongoDB?

Numerical values that are stored as Int32 in mongosh would have been stored by default as Double in the mongo shell. The Int32() constructor can be used to explicitly specify 32-bit integers.


2 Answers

To list databases try :

function sd(){
     return db._adminCommand( { listDatabases: 1 } ) 
}

Basically you have to run valid javascript here. Remember that you have to run these in context of admin database - runCommand will not be enough - you have to use _adminCommand here.

For other commands see http://docs.mongodb.org/manual/reference/command/

if you would like to get rid of parenthesis there is also a way (you would have to put property on 'this')

Object.defineProperty(this, 'sd', {
    get: function() { 
        return db._adminCommand( {listDatabases: 1} )
    },
    enumerable: true,
    configurable: true
});
like image 76
marcinn Avatar answered Oct 05 '22 23:10

marcinn


Another way to alias databases in the mongo shell is to use the db.getSiblingDB() command. In the following, imagine you have two MongoDB databases - AmazonSales and EbaySales. Both the databases have a users collection. You can now use the aliases as described below in the Mongo shell, instead of always needing the 'use ' command to switch context

var cs = db.getSiblingDB('AmazonSales')
cs.users.count()
cs.users.find({name:'John'})

var r = db.getSiblingDB('EbaySales')
r.users.count()
like image 42
ice.nicer Avatar answered Oct 06 '22 01:10

ice.nicer