PyMongo returns 浮点数而不是整数

PyMongo returns float instead of integer

我在 Mongo 中有一个集合,我在其中存储 URL,为此我使用以下字段:

在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.

我猜值存储​​为浮点数。

相关: