How do we print a variable at the MongoDB command prompt?


In order to print a variable at the MongoDB command prompt, use the following syntax

//Declaring and Initializing a variable.
var anyVariableName=yourValue;
//To print the above variable.
yourVariableName;
Or
print(yourVariableName);

Following is how you can declare and initialize a variable at the MongoDB command prompt

> var myIntegerValue=20;

Print a variable at the MongoDB command prompt:

> myIntegerValue

This will produce the following output

20

You can also use print(). Following is the query

> print(myIntegerValue);

This will produce the following output

20

Let us see another example. Following is the query to declare and initialize a string variable at MongoDB shell.

> var myStringValue="Hello MongoDB!!!";

Following is the query to print a variable at the MongoDB command prompt.

> myStringValue

This will produce the following output

Hello MongoDB!!!

You can also use print(). Following is the query

> print(myStringValue);

This will produce the following output

Hello MongoDB!!!

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements