Perl MongoDB driver number handling

Dieser Post wurde aus meiner alten WordPress-Installation importiert. Sollte es Darstellungsprobleme, falsche Links oder fehlende Bilder geben, bitte einfach hier einen Kommentar hinterlassen. Danke.

Perl doesn't care about numbers, integer or float - at least on the script level - but MongoDB does. The driver's default number detection algorithm treats floats as strings and MongoDB doesn't like to compare strings with less-than or greater-than operations.

The MongoDB driver for Perl is pretty much written like Java classes in Perl - there are Modules for common (maybe all) data types but Perl developers are used to work with scalars instead of "strings", "integers", "floats" or other creatures limiting the free flow of data.

The driver authors added a cheap trick to handle everything which looks like a number (based on Perl's commonly used function from Scalar::Util) as numbers and anything which is no number as string, but it's hidden in the MongoDB POD. Add this line to the initialization phase of your script or framework:

$MongoDB::BSON::looks_like_number = 1;
Everything which looks like a number will be stored as a number from now on which might also introduce post code or phone number problems if they have leading zeros (but these problems aren't anything now to most Perl developers).

The same MongoDB POD page (linked above) provides solutions for most other data type dependent problems. MongoDB could handle DateTime values, but I always prefer to store Unix timestamps (epoch based), sometimes even on SQL.


Noch keine Kommentare. Schreib was dazu

Schreib was dazu

Die folgenden HTML-Tags sind erlaubt:<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>