Uncaught exception 'MongoException' with message 'Invalid object ID'

mongodb, php

Solution

Most probably this error is because of the wrong `$some` you are providing. Now you have to pass a correct MongoID to `new MongoId()` constructor.

So `new MongoId('51e1eefc065f908c10000411')` will be ok, but `new MongoId('-6')` will generate your error.

I am using try catch to handle this.

try {
    $something = new MongoId($some);
} catch (MongoException $ex) {
    $something = new MongoId();
}

So I think that this documentation is a little bit outdated and should be changed.

Problem

I had a PHP code which was working nicely, but after I updated Mongo (2.4.4) and mongo driver for php (1.4.2), my code started to generate fatal error. The line which generates an error is this one ``` $something = new MongoId($some); ``` It generates an error: ``` Fatal error: Uncaught exception 'MongoException' with message 'Invalid object ID' ``` Surely, I can roll back my updates, but is there any idea how I can fix it without rolling back?

Original source