Felipe Plets
Felipe Plets

Reputation: 7520

PyMongo returns float instead of integer

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

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?

Upvotes: 3

Views: 3327

Answers (2)

Jérôme
Jérôme

Reputation: 14714

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

Upvotes: 5

Muatik
Muatik

Reputation: 4171

you can cast it to integer like this:

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

Upvotes: 0

Related Questions