How to define aliases in the MongoDB Shell?

MongoDBDatabaseBig Data Analytics

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
raja
Published on 23-May-2019 11:46:37
Advertisements