MongoDB and 64-bit Integers in PHP

in PHP, Twitter

If you’ve ever needed to work with PHP’s MongoDB driver and large integers (which Twitter and Facebook use for id’s) you might have run into a problem …

Derick Rethans documents this problem at length at his post here

[A] Facebook UserID … [uses] a “64-bit int datatype”.

Unfortunately, the MongoDB PHP Driver only had support for 32-bit integers causing problems for newer users of Facebook. For those users, their nice long UserID was truncated to only 32 bits which didn’t quite make the application work.

Basically, while MongoDB itself supports very large 64 bit numbers the PHP driver only support(ed) up to 32 bits … so when you inserted into MongoDB via PHP you’d get a truncated (cutoff) number.

The good news is this has now been fixed in driver version 1.0.9 (released 8/6/2010) and up, however you might be wondering:

  • How can I tell which driver I have?
  • How do I upgrade?
  • How do I configure my PHP settings to allow for 64 bit numbers?

Check Your Version Number

One very quick and easy way to check your Mongo PHP driver version is to create a .php file on your server, add the following and save …

<?php
    phpinfo();
?>

Then open the page in your web browser and look for “Mongo” (or use ctrl-f for find) … you should find something like this …

If you see Version 1.0.8 or lower you need to upgrade …

Keep It Simple, Upgrade

If you already have the PHP Mongo driver installed the easiest thing to do is simply run an upgrade. If you are running Linux you can use pecl, on Windows you can download and replace the .dll file.

If you have not done so, see our earlier post on the subject: MongoDB+PHP: Install and Connect

Via your Linux command line run …

# sudo pecl upgrade mongo

This should fetch the latest Mongo PHP driver and install it for you, if you already have the latest it will just exit. You will need to restart your webserver for the changes to take effect.

To see if it worked you can use the phpinfo() method above.

Oh No, It Errored

If your server does not allow executions on /tmp you might get an error like this …

/usr/bin/phpize: 209:
/tmp/pear/temp/mongo/build/shtool: Permission denied

If so, you need to download and run the upgrade manually (in some other dir than /tmp/ …) Download the latest drivers here and follow the straighforward directions. It’s pretty easy, don’t worry!

How To Set PHP .ini Properly

Depending on the version of the driver you have installed (new versions will have this set by default) you may need to turn on support for 64 long integers.

You can either do this in your php.ini or inline in your code. Edit your php.ini file and add the following line:

[mongodb]
mongo.native_long = 1

Or, do this in code like so:

<?php
ini_set('mongo.native_long', 1);

//code here
?>

I Can’t Upgrade? :(

For some users upgrading might not be an option.

Luckily there is a work around: you can simple insert the 64-bit integer as a strings using quotes when you insert. You can also user a regular expression to help you.

Say you have a JSON array from Twitter with these large numbers … you can use the following regex to change them to strings …

$usertimeline = preg_replace('/id":(\d+)/', 'id":"\1"', $usertimeline);

While not the most optimized approach … if upgrading is not an option it maybe your best bet.

5 Comments