PyMongo returns 浮点数而不是整数
PyMongo returns float instead of integer
我在 Mongo 中有一个集合,我在其中存储 URL,为此我使用以下字段:
- 协议(类型:字符串)- 例如:https
- 主机(类型:字符串)- 例如:google.com
- 端口(类型:整数)- 例如:80
在mongo客户端我执行
db.myCollection.find()
它returns
{"protocol" : "https", "host" : "google.com", "port" : 80}
在 Python 中使用 PyMongo 我执行
for service in myCollection.find():
print service['port']
它returns
80.0
我的号码存储为整数吗?
使用 PyMongo,我怎样才能 return 它是整数而不是浮点数?
您可以像这样将其转换为整数:
for service in myCollection.find():
print int(service['port'])
来自the docs:
By default, the mongo shell treats all numbers as floating-point
values. The mongo shell provides the NumberInt() constructor to
explicitly specify 32-bit integers.
我猜值存储为浮点数。
相关:
我在 Mongo 中有一个集合,我在其中存储 URL,为此我使用以下字段:
- 协议(类型:字符串)- 例如:https
- 主机(类型:字符串)- 例如:google.com
- 端口(类型:整数)- 例如:80
在mongo客户端我执行
db.myCollection.find()
它returns
{"protocol" : "https", "host" : "google.com", "port" : 80}
在 Python 中使用 PyMongo 我执行
for service in myCollection.find():
print service['port']
它returns
80.0
我的号码存储为整数吗?
使用 PyMongo,我怎样才能 return 它是整数而不是浮点数?
您可以像这样将其转换为整数:
for service in myCollection.find():
print int(service['port'])
来自the docs:
By default, the mongo shell treats all numbers as floating-point values. The mongo shell provides the NumberInt() constructor to explicitly specify 32-bit integers.
我猜值存储为浮点数。
相关: