Mongodb 2.6.0-rc2 and PHP 1.4.5 - find _id $in Mongodb 2.6.0-rc2 and PHP 1.4.5 - find _id $in php php

Mongodb 2.6.0-rc2 and PHP 1.4.5 - find _id $in


This... is a change in MongoDB 2.6.0, no longer accepting bson object in the $in clause.

This particular issue is being tracker as a PHP driver bug at https://jira.mongodb.org/browse/PHP-1051

The MongoDB PHP Driver will serialize an PHP Array into BSON Array (accepted by the $in operator) when the PHP array is: Sequential numerically indexed, starting from 0

This means that if you have an array like:

$array = array($id0, $id1, $id2, $id3, $id4);

and then you

unset($array[0]);

You actually wind up with:

$array = array(1 => $id1, 2 => $id2, 3 => $id3, 4 => $id);

Which does not start with index 0. The MongoDB PHP driver therefore converts this into a BSON Object... Leading to validation error in MongoDB as it expected an array.

Now, since the MongoDB PHP driver does not do parse your MongoDB query we cannot know which array should be exempted from this serialization rule.

The workaround is, as mentioned above, is to ensure your PHP arrays are numerically indexed, starting from 0. The easiest way to do that is to run

array_values($values);


After debugging it with mongo, I've noticed the following:

... query: { _id: { $in: { 0: ObjectId('52214d60012f8aab278eaad1') ...

but it should be

... query: { _id: { $in: [ ObjectId('52214d60012f8aab278eaad1') ...

So, I would suggest doing array_values($VAR) before calling $in, I'm guess Mongo 2.4.9 is more lenient to the fact that $ids can be either object or an array, make sure you use arrays in mongo 2.6 ;)