Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How do we print a variable at the MongoDB command prompt?
To print a variable at the MongoDB command prompt, you can either reference the variable name directly or use the print() function. Both methods display the variable's value in the shell.
Syntax
// Declaring and initializing a variable var variableName = value; // Method 1: Direct variable reference variableName; // Method 2: Using print() function print(variableName);
Example 1: Integer Variable
Declare and initialize an integer variable ?
var myIntegerValue = 20;
Print the variable using direct reference ?
myIntegerValue;
20
Print using the print() function ?
print(myIntegerValue);
20
Example 2: String Variable
Declare and initialize a string variable ?
var myStringValue = "Hello MongoDB!!!";
Print the variable using direct reference ?
myStringValue;
Hello MongoDB!!!
Print using the print() function ?
print(myStringValue);
Hello MongoDB!!!
Key Points
- Both methods produce identical output for simple data types.
-
print()is more explicit and readable in scripts. - Direct variable reference is quicker for interactive shell use.
Conclusion
MongoDB shell provides two simple ways to display variable values: direct reference and the print() function. Choose based on your preference and context.
Advertisements
