Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the MongoDB CPAN distribution.

Maintainer(s)' notes

Please don't report bugs here. Please use the MongoDB Perl driver issue tracker instead.

Report information
The Basics
Id: 57005
Status: resolved
Priority: 0/
Queue: MongoDB

People
Owner: Nobody in particular
Requestors: whatson [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: Inconsistent handling of integers
Date: Wed, 28 Apr 2010 17:32:17 +1000
To: bug-MongoDB [...] rt.cpan.org
From: Andrew Whatson <whatson [...] gmail.com>
Hi, It seems that the handling of numeric values is not quite as-advertised in the MongoDB documentation. I've attached a perl script that inserts a variety of test values, and then connects to the database via the mongo shell to dump the test values as mongo sees them. The results of running the script indicate that bare integers (ie. unquoted integer literals, eg. 3) are stored as 'floatApprox' values. Conversely, integers with arbitrary decimal places (eg. 3.0) are stored as bare integers. I'm not sure if this is an issue with the driver or the documentation, though my guess would be the former. Regards, Andrew

Message body is not shown because sender requested not to inline it.

JavaScript only has one number type, so we've hacked around that limitation a bit in the Mongo shell. I'm not sure what you're seeing, I get: { "_id" : ObjectId("4bd8385cb93b22124b50bebd"), "test" : "bare integers", "a" : 1, "b" : 2, "c" : 3 } { "_id" : ObjectId("4bd8385c0f901d124beb94f5"), "test" : "bare floats", "a" : 1.5, "b" : 2.5, "c" : 3.5 } { "_id" : ObjectId("4bd8385c52275b124bf1c503"), "test" : "integers as bare floats", "a" : 1, "b" : 2, "c" : 3 } { "_id" : ObjectId("4bd8385cc819ad124bbbb7ad"), "test" : "integers as strings", "a" : "1", "b" : "2", "c" : "3" } { "_id" : ObjectId("4bd8385cbd09e7124b7a2c8f"), "test" : "floats as strings", "a" : "1.5", "b" : "2.5", "c" : "3.5" } when I run your script, which looks right to me. Also, when I dump the documents in a language that actually has two different numeric types (PHP), it looks like they're all being stored as the correct type: array(5) { ["_id"]=> object(MongoId)#7 (0) { } ["test"]=> string(13) "bare integers" ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) } array(5) { ["_id"]=> object(MongoId)#8 (0) { } ["test"]=> string(11) "bare floats" ["a"]=> float(1.5) ["b"]=> float(2.5) ["c"]=> float(3.5) } array(5) { ["_id"]=> object(MongoId)#7 (0) { } ["test"]=> string(23) "integers as bare floats" ["a"]=> float(1) ["b"]=> float(2) ["c"]=> float(3) } array(5) { ["_id"]=> object(MongoId)#8 (0) { } ["test"]=> string(19) "integers as strings" ["a"]=> string(1) "1" ["b"]=> string(1) "2" ["c"]=> string(1) "3" } array(5) { ["_id"]=> object(MongoId)#7 (0) { } ["test"]=> string(17) "floats as strings" ["a"]=> string(3) "1.5" ["b"]=> string(3) "2.5" ["c"]=> string(3) "3.5" } Can you explain what you're seeing that looks wrong?
Subject: Re: [rt.cpan.org #57005] Inconsistent handling of integers
Date: Thu, 29 Apr 2010 09:16:27 +1000
To: bug-MongoDB [...] rt.cpan.org
From: Andrew Whatson <whatson [...] gmail.com>
Hi Kristina, On two different test machines (CentOS and ArchLinux) both with perl 5.10.1, MongoDB perl driver 0.33 and MongoDB 1.4.1 I get the following results: { "_id" : ObjectId("4bd8bed5f383ca3272ed1f22"), "test" : "bare integers", "a" : { "floatApprox" : 1 }, "b" : { "floatApprox" : 2 }, "c" : { "floatApprox" : 3 } } { "_id" : ObjectId("4bd8bed5ec5d393272dd8fd9"), "test" : "bare floats", "a" : 1.5, "b" : 2.5, "c" : 3.5 } { "_id" : ObjectId("4bd8bed57299423272b482f1"), "test" : "integers as bare floats", "a" : 1, "b" : 2, "c" : 3 } { "_id" : ObjectId("4bd8bed55a10723272e44125"), "test" : "integers as strings", "a" : "1", "b" : "2", "c" : "3" } { "_id" : ObjectId("4bd8bed5af780d32720cea37"), "test" : "floats as strings", "a" : "1.5", "b" : "2.5", "c" : "3.5" } It's the 'floatApprox' that really seems odd, and inconsistent with how integers are treated when entered as bare floats. Regards, Andrew
Subject: Re: [rt.cpan.org #57005] Inconsistent handling of integers
Date: Fri, 30 Apr 2010 11:42:45 +1000
To: bug-MongoDB [...] rt.cpan.org
From: Andrew Whatson <whatson [...] gmail.com>
Hi Kristina, It might also be relevant that both test machines are 64bit. Regards, Andrew
This is because Perl does not distinguish between 32- and 64-bit integers, so ints are always saved at 64-bit ints on 64-bit machines. As the shell doesn't have anything other than a float type, it can't be sure it can represent 64-bit ints exactly, hence the floatApprox. It's just letting you know that it's a 64-bit int, so it might be a little off as a float. I can add some more documentation about how the shell to the MongoDB::DataTypes file.
Updated the docs.