Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB inserts float when trying to insert integer

Tags:

mongodb

People also ask

What is Int32 in MongoDB?

Numerical values that are stored as Int32 in mongosh would have been stored by default as Double in the mongo shell. The Int32() constructor can be used to explicitly specify 32-bit integers.

What is NumberInt in MongoDB?

NumberInt. The mongo shell treats all numbers as floating-point values by default. The mongo shell provides the NumberInt() constructor to explicitly specify 32-bit integers.

Which of the following is correct command to insert data into MongoDB?

The insert() Method To insert data into MongoDB collection, you need to use MongoDB's insert() or save() method.


db.data.update({'name': 'zero'}, {'$set': {'value': NumberInt(0)}})

You can also use NumberLong.


A slightly simpler syntax (in Robomongo at least) worked for me:

db.database.save({ Year : NumberInt(2015) });

If the value type is already double, then update the value with $set command can not change the value type double to int when using NumberInt() or NumberLong() function. So, to Change the value type, it must update the whole record.

var re = db.data.find({"name": "zero"})
re['value']=NumberInt(0)
db.data.update({"name": "zero"}, re)