python插入mongodb整数问题

如果我们用python插入一条内容,里面有一个整数

dict = { "name": "test", "num": 1} 
col.insert_one(dict)

得到的结果如下

{
    "name": "test",
    "num": NumberInt("1")
}

如果是这样书写

dict = { "name": "test", "num": 1.0} 
col.insert_one(dict)

就会得到这样的结果

{
    "name": "test",
    "num": 1
}

这是因为什么呢?第一个是不是数字类型呢?

第一个是数字类型,这是因为mongodb中只有一个数字类型,没有区分整数和小数,参考https://www.mongodb.com/docs/v6.0/core/shell-types/?_ga=2.254942348.549717307.1660180415-1586432957.1660180415

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.

所以说如果是1.0,就默认不需要特殊处理,如果是1,就需要增加NumberInt()来限制大小

posted @ 2022-08-11 16:31  秋来叶黄  阅读(60)  评论(0编辑  收藏  举报