Subject: | Numbers may arrive as parameters to MongoDB as Strings, or vice-versa! |
Numbers in Perl may arrive as parameters to MongoDB as Strings. Without
normalization, this can cause the appearance of lost data or data
duplication.
While this might be a feature, you may consider documenting this
behavior as an option.
======= Code To Reproduce Bug on 0.39 ========
use MongoDB;
use MongoDB::Connection;
use strict;
print "MongoDB Version: $MongoDB::VERSION\n";
my $m = MongoDB::Connection->new;
my $db = $m->test_db;
my $c = $db->test_collection;
# Integers
$c->insert( { x => 123 } );
my $ret = $c->find_one( { x => '123' } );
print "can NOT find '123' because it was stored as an integer\n" if
(!defined($ret));
$ret = $c->find_one( { x => 123 } );
print "CAN find 123 because it was stored as an integer\n" if (
defined($ret));
# Floats
$c->insert( { x => 123.45 } );
$ret = $c->find_one( { x => '123.45' } );
print "can NOT find '123.45' because it was stored as a float\n" if
(!defined($ret));
$ret = $c->find_one( { x => 123.45 } );
print "CAN find 123.45 because it was stored as a float\n" if (
defined($ret));
====== Output ======
MongoDB Version: 0.39
can NOT find '123' because it was stored as an integer
CAN find 123 because it was stored as an integer
can NOT find '123.45' because it was stored as a float
CAN find 123.45 because it was stored as a float
====== Perl Versions ======
Tested using Perl v5.10.1 as distributed by Ubuntu 10.04
Same result when using ActivePerl v5.12.2
====== Possible Solutions =====
1. Force all values to cast as a string:
$string = '' . $x
2. Create MongoDB::Number-like datatype objects for explicit typing into
the MongoDB. (If you do this, #1 should be default, IMO)
3. Attempt to guess the data type and cast it appropriately when storing
it in MongoDB. (this could be slow)
4. Do something amazing that only the great insight of the maintainer
would know of, that would resolve this for every possible use of the
perl MongoDB module!
Thank you for your help!
-Eric