How to Downgrade PECL MongoDB Driver to Specific Version (1.3.7) for Backward Compatibility


2 views

When working with MongoDB in PHP, version mismatches between the PECL driver and MongoDB server can cause unexpected behavior. In this case, we're seeing an Invalid object ID exception when trying to create a MongoId object, despite the code working perfectly on another environment.

The working environment has:

mongo   1.3.7   stable
mongod  2.4.3

While the failing environment has:

mongo   1.5.6   stable
mongod  2.6.0

Here's how to properly downgrade the MongoDB driver:

# First remove current version
pecl uninstall mongo

# Then install specific version
pecl install mongo-1.3.7

If you encounter dependency issues, you might need to:

# Install with force flag
pecl install -f mongo-1.3.7

# Or specify the full URL
pecl install http://pecl.php.net/get/mongo-1.3.7

If you're using Composer, you can specify the driver version in your composer.json:

{
    "require": {
        "ext-mongo": "1.3.7"
    }
}

The MongoDB server version difference (2.4.3 vs 2.6.0) might also contribute to the issue. Consider:

  1. Using the same MongoDB server version across environments
  2. Updating your code to handle both versions

Here's a more version-tolerant version of your code:

// Safely create MongoId from string
try {
    $passengerId = new \MongoId((string)$oPassenger->getId());
} catch (MongoException $e) {
    throw new \RuntimeException('Invalid passenger ID format');
}

return $this->createQueryBuilder('Device')
    ->update()
    ->multiple(true)
    ->field('activated')->set(false)
    ->field('passenger')->unsetField()->equals($passengerId)
    ->field('_id')->notEqual($deviceId)
    ->getQuery()
    ->execute();

After installation, verify the driver version:

php -r "echo phpversion('mongo');"

Should output: 1.3.7

Remember that the old MongoDB driver (mongo) is deprecated in favor of the new mongodb driver. For new projects, consider migrating to:

pecl install mongodb

But for legacy systems, maintaining version consistency is crucial.


When working with MongoDB in PHP, you might encounter compatibility issues between different versions of the PECL MongoDB driver and MongoDB server. In this case, we're dealing with:

MongoException: "Invalid object ID"

The key difference between environments:

// Development server
mongo   1.3.7   stable
db version v2.4.3

// Local machine
mongo   1.5.6   stable
db version v2.6.0

The MongoDB PHP driver 1.3.7 was designed to work optimally with MongoDB server 2.4.x, while version 1.5.x has changes that might affect object ID handling with newer server versions. The error occurs specifically in this code:

$passengerId = new \MongoId(oPassenger->getId());

Here's how to correctly install MongoDB driver 1.3.7:

# First uninstall current version
sudo pecl uninstall mongo

# Install specific version with pecl
sudo pecl install mongo-1.3.7

# Alternatively, download and compile manually
wget http://pecl.php.net/get/mongo-1.3.7.tgz
tar -xzf mongo-1.3.7.tgz
cd mongo-1.3.7
phpize
./configure
make
sudo make install

After installation, confirm the version is correct:

php -r "echo phpversion('mongo');"

Should output: 1.3.7

If you're using Composer, you can specify the exact version in composer.json:

{
    "require": {
        "ext-mongo": "1.3.7"
    }
}

If downgrading isn't feasible, consider these code modifications:

1. Validate object IDs before use:

try {
    $passengerId = new \MongoId($oPassenger->getId());
} catch (MongoException $e) {
    // Handle invalid ID case
}

2. Use the newer MongoDB driver (1.5.6+) with proper ID handling:

// For newer versions, consider using MongoDB\BSON\ObjectID
$passengerId = new MongoDB\BSON\ObjectID($oPassenger->getId());