How to define aliases in the MongoDB Shell?


To define aliases in MongoDB shell, you can use below syntax −

Object.defineProperty(this, 'yourFunctionName', {
   get: function() {
      yourStatement1,
      .
      .

      return N
   },
   enumerable: true,
   configurable: true
});

Following is the syntax to assign with var −

var anyAliasName=yourFunctionName;

Let us implement the above syntax in order to define an aliases in the MongoDB shell. Here, 'displayMessageDemo' is our function −

> Object.defineProperty(this, 'displayMessageDemo', {
...   get: function() {
...      return "Hello MongoDB"
...   },
...   enumerable: true,
...   configurable: true
... });

Query to assign function to var in MongoDB shell −

> var myMessage = displayMessageDemo;

Let us display the value of above aliases −

> myMessage;

This will produce the following output −

Hello MongoDB

Updated on: 30-Jul-2019

146 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements