Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one print a variable at the MongoDB command prompt?

Tags:

mongodb

How does one print a variable at the MongoDB command prompt. Unbelievably, I cannot figure this out.

like image 682
cammil Avatar asked Aug 10 '11 14:08

cammil


2 Answers

> var i = 0;
> i
0
> print(i)
0
>

Use either as appropriate. Happens to the best of us ;)

like image 67
Remon van Vliet Avatar answered Oct 21 '22 23:10

Remon van Vliet


You can execute Java Script in Mongo CLI or command prompt The print() method simply prints the data arguments that are passed into it. print("Hello From Mongo");

eg:

print(data, ...);
printjson(object);
print(JSON.stringify(object));

The printjson() method prints a pretty form of the JavaScript object. The print(JSON.stringify(object)) method also prints a tightly compact string form of a JavaScript object.

like image 36
SaM Avatar answered Oct 21 '22 22:10

SaM