Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyMongo returns float instead of integer

I have a collection in Mongo in which I store URLs, for that I use the following fields:

  • protocol (type: string) - e.g.: https
  • host (type: string) - e.g.: google.com
  • port (type: integer) - e.g.: 80

In mongo client I execute

db.myCollection.find()

and it returns

{"protocol" : "https", "host" : "google.com", "port" : 80}

In Python with PyMongo I execute

for service in myCollection.find():
  print service['port']

and it returns

80.0

Is my number stored as integer?

Using PyMongo, how can I return it as integer and not float?

like image 477
Felipe Plets Avatar asked Apr 08 '26 21:04

Felipe Plets


1 Answers

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

I guess value is stored as float.

Related: https://stackoverflow.com/a/30501514/4653485

like image 149
Jérôme Avatar answered Apr 10 '26 11:04

Jérôme



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!