MongoDB inserts float when trying to insert integer

前端 未结 4 1193
小蘑菇
小蘑菇 2020-12-12 13:08
   > db.data.update({\'name\': \'zero\'}, {\'$set\': {\'value\': 0}}) 
   > db.data.findOne({\'name\': \'zero})
    {\'name\': \'zero\', \'value\': 0.0}  
         


        
4条回答
  •  南方客
    南方客 (楼主)
    2020-12-12 13:41

    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)
    

提交回复
热议问题