PHP Driver
  1. PHP Driver
  2. PHP-104

A PHP array with non-consecutive, numeric keys becomes an object in mongodb

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Minor - P4 Minor - P4
    • Resolution: Won't Fix
    • Affects Version/s: 1.0.6
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:
      php 5.3.1 windows
      mondo 1.0.6 driver
    • # Replies:
      1
    • Last comment by Customer:
      true

      Description

      php example:

      $array = array('a', 'b', 'c');
      unset($array[1]);

      $document = array(
      'embedded' => $array,
      );

      // assuming local
      $mongo = new Mongo();
      $mongo->test->test->insert($document);

      mongodb result:
      { "_id" : ObjectId(...), "embedded" :

      { "0" : "a", "2" : "c" }

      }

      expected:
      ['a', 'c']

      I'm not sure how checking is done at the moment by the driver, but at the point of insert, all keys of the array are still ints, even if they are not consecutive. So, perhaps the difference maker between an object and an array is whether the keys consist of all ints (array), or a mixture of ints and strings etc. (object).

      My workaround for now is to use anything that will 'compact' the keys, like sort(), before inserting arrays.

      I'm aware that people could argue for both behaviours, so I'm logging this bug with the following in mind:

      • if you're not aware, make you aware of the issue
      • if you are aware, to get a relatively final response logged in JIRA as to whether it will be changed, or if it's a wont-fix
      • if it's a wont-fix, to obtain official recommendation of workaround

        Issue Links

          Activity

          • Comments
          Hide
          Kristina Chodorow (Inactive)
          added a comment -
          • if you're not aware, make you aware of the issue

          Yes, I am aware. It is a consequence of overloading array()s to be objects and arrays.

          • if you are aware, to get a relatively final response logged in JIRA as to whether it will be changed, or if it's a wont-fix

          As mentioned in the related user group thread, it's a speed optimization. The driver has to do zero work this way to tell if something is an array, it just sort of falls out as part of the serialization process.

          There is a disconnect between what an array is in PHP and what it is in Mongo, unfortunately. Mongo won't treat something as an array (that is, allow you to do array ops like $push and $pull) on it unless it has 0-based ascending numeric indexes. Thus, the driver uses the stricter Mongo definition of an array.

          Originally the driver did not enforce this, but then you had this semi-useless object that you couldn't use as an array (because Mongo knew it was the wrong "shape") and you couldn't use as an object (because it was an array type).

          • if it's a wont-fix, to obtain official recommendation of workaround

          Use array_splice to remove elements from an array. That will re-index the array correctly. (http://www.php.net/manual/en/function.array-splice.php)

          Show
          Kristina Chodorow (Inactive)
          added a comment - if you're not aware, make you aware of the issue Yes, I am aware. It is a consequence of overloading array()s to be objects and arrays. if you are aware, to get a relatively final response logged in JIRA as to whether it will be changed, or if it's a wont-fix As mentioned in the related user group thread, it's a speed optimization. The driver has to do zero work this way to tell if something is an array, it just sort of falls out as part of the serialization process. There is a disconnect between what an array is in PHP and what it is in Mongo, unfortunately. Mongo won't treat something as an array (that is, allow you to do array ops like $push and $pull) on it unless it has 0-based ascending numeric indexes. Thus, the driver uses the stricter Mongo definition of an array. Originally the driver did not enforce this, but then you had this semi-useless object that you couldn't use as an array (because Mongo knew it was the wrong "shape") and you couldn't use as an object (because it was an array type). if it's a wont-fix, to obtain official recommendation of workaround Use array_splice to remove elements from an array. That will re-index the array correctly. ( http://www.php.net/manual/en/function.array-splice.php )

            People

            • Assignee:
              Kristina Chodorow (Inactive)
              Reporter:
              bvnx
              Participants:
              Last commenter:
              Jason Rassi
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since reply:
                4 years, 3 days ago
                Date of 1st Reply: