Home > Database > MongoDB > How to define aliases in MongoDB Shell?

How to define aliases in MongoDB Shell?

王林
Release: 2023-09-13 10:41:06
forward
1416 people have browsed it

如何在 MongoDB Shell 中定义别名?

To define an alias in the MongoDB shell you can use the following syntax -

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

      return N
   },
   enumerable: true,
   configurable: true
});
Copy after login

The following is the syntax for assignment using var -

var anyAliasName=yourFunctionName;
Copy after login

Let We implement the above syntax to define aliases in the MongoDB shell. Here, "displayMessageDemo" is our function -

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

Query to assign function to var in MongoDB shell -

> var myMessage = displayMessageDemo;
Copy after login

Let us display the value of the above alias -

> myMessage;
Copy after login

This will produce the following output -

Hello MongoDB
Copy after login

The above is the detailed content of How to define aliases in MongoDB Shell?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template