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


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)

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

173 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements