1. http://stackoverflow.com/questions/22748000/mongodb-2-6-0-rc2-and-php-1-4-5-find-id-in
    2. A simple query like this:
    3. $a = array('_id' => array( '$in' => array_values($ids) ) );
    4. var_dump($a);
    5. $cursor2 = $data->find( $a );
    6. works in mongodb 2.4.9, however, in 2.6.0-rc2 returns this:
    7. Type: MongoCursorException
    8. Code: 17287
    9. Message: Can't canonicalize query: BadValue $in needs an array
    10. The output from var_dump:
    11. array(1) {
    12. ["_id"]=>
    13. array(1) {
    14. ["$in"]=>
    15. array(10) {
    16. [0]=>
    17. object(MongoId)#57 (1) {
    18. ["$id"]=>
    19. string(24) "52214d60012f8aab278eacb6"
    20. }
    21. [1]=>
    22. object(MongoId)#58 (1) {
    23. ["$id"]=>
    24. string(24) "52214d60012f8aab278eaca8"
    25. }
    26. [2]=>
    27. object(MongoId)#59 (1) {
    28. ["$id"]=>
    29. string(24) "52214d60012f8aab278eaca7"
    30. }
    31. }
    32. }
    33. }
    34. I wonder if this is a Mongo or PHP related?
    35. THanks!
    36. php mongodb mongodb-php
    37. shareimprove this question
    38. asked Mar 30 '14 at 18:28
    39. Pavel
    40. 1831112
    41. add a comment
    42. 2 Answers
    43. active oldest votes
    44. up vote
    45. 9
    46. down vote
    47. This... is a change in MongoDB 2.6.0, no longer accepting bson object in the $in clause.
    48. This particular issue is being tracker as a PHP driver bug at https://jira.mongodb.org/browse/PHP-1051
    49. 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
    50. This means that if you have an array like:
    51. $array = array($id0, $id1, $id2, $id3, $id4);
    52. and then you
    53. unset($array[0]);
    54. You actually wind up with:
    55. $array = array(1 => $id1, 2 => $id2, 3 => $id3, 4 => $id);
    56. 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.
    57. 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.
    58. 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
    59. array_values($values);
    60. shareimprove this answer
    61. edited Jul 31 at 9:43
    62. Kevin Yan
    63. 586315
    64. answered Apr 9 '14 at 0:49
    65. bjori
    66. 1,276158
    67. -1 Sorry, but check the code in question, array_values is used already. - Imran Naqvi Aug 28 '14 at 9:52
    68. This answer helped me with a problem I had using jenssegers/laravel-mongodb vendor for Laravel. I am posting an issue on their Gitgub account with a reference to this answer for support. Thanks! - rayVenues Sep 30 '14 at 12:28
    69. add a comment
    70. up vote
    71. 2
    72. down vote
    73. After debugging it with mongo, I've noticed the following:
    74. ... query: { _id: { $in: { 0: ObjectId('52214d60012f8aab278eaad1') ...
    75. but it should be
    76. ... query: { _id: { $in: [ ObjectId('52214d60012f8aab278eaad1') ...
    77. 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 ;)
    78. shareimprove this answer
    79. answered Apr 3 '14 at 21:35
    80. Pavel
    81. 1831112
    82. nice catch with array_values - Thiago F Macedo Apr 24 '14 at 13:17
    83. -1, Sorry but please check in the code, array_values is used. - Imran Naqvi Aug 28 '14 at 9:50
    84. add a comment