Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Does Mongo shell treats numbers as float by default.? How can we work it around explicitly?


1 Answer
Samual Sam

Yes, Mongo shell treats numbers as float by default. To work it as int or any other type, you need to mention explicitly. You can use NumberInt() for this. The syntax is as follows −

var anyVariableName= [NumberInt("yourValue1"), NumberInt("yourValue2"),.....N];

Let us implement the above syntax in order to treat numbers as integer only (not float) −

> var integerArrayDemo = [NumberInt("50"), NumberInt("60"),
   NumberInt("70"),NumberInt("90"),NumberInt("40")];

Following is the query to display the array value −

> printjson(integerArrayDemo);

This will produce the following output −

[
   NumberInt(50),
   NumberInt(60),
   NumberInt(70),
   NumberInt(90),
   NumberInt(40)
]

To display the array value, you can use print() −

> print(integerArrayDemo);

This will produce the following output −

NumberInt(50),NumberInt(60),NumberInt(70),NumberInt(90),NumberInt(40)

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.