- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- How to print document value in MongoDB shell?
- How to unset a variable in MongoDB shell?
- How to get all the aliases in PowerShell?
- How to terminate a MongoDB shell script earlier?
- How to run MongoDB shell using mongos command?
- How to operate on all databases from the MongoDB shell?
- Prettyprint in MongoDB shell as default?
- Format date value in MongoDB shell?
- Inserting Date() in MongoDB through Mongo shell?
- Use result from MongoDB in shell script?
- Get output of MongoDB shell script?
- How to insert an 8-byte integer into MongoDB through JavaScript shell?
- Variable collection name in MongoDB shell with JavaScript?
- How to list down all the available commands and aliases on Linux?
- How to get items with a specific value from documents using MongoDB shell?

Advertisements