MongoDB $set not updating record

After performing various tests, based on the comment from yi_H and answer from nnythm I have found the following.

In all cases I am using this common code:

$collection->update(
    array('_id' => $obj['_id']),
    array('$set' => $updateObj)
);

The following do not work at all:

  • $updateObj = array(0 => 100);
  • $updateObj = array('0' => 100);

These do work:

  • $updateObj = array(1 => 100);
  • $updateObj = array('1' => 100);

After a bit of googling and reading some of the Mongo PHP docs, I found I can use objects instead of arrays. So I tried this:

$updateObj = new stdClass;
$updateObj->{0} = 100;

THIS WORKS!

But I haven't been able to find out why...

Edit:

Poking through the mongo extension source code

The MongoCollection->update method performs the following, buf is already a pointer and newobj is a zval (the second parameter of the query). HASH_P simply returns the right property of the zval for encoding, depending on whether it is an array or an object.

zval_to_bson(buf, HASH_P(newobj), NO_PREP TSRMLS_CC)

The bson_encode function performs the following, identical in terms of functionality. buf pointer and zval z.

zval_to_bson(&buf, HASH_P(z), 0 TSRMLS_CC);

So I performed the following test.

$updateObj = new stdClass;
$updateObj->{0} = 100;

$one = bson_encode($updateObj);

$updateObj = array(0 => 100);

$two = bson_encode($updateObj);

var_dump($one === $two);

The output is true

Still at a loss why 0 isn't working for a field name in an array.

Edit 2:

A further experiment shows that when a field with a name of 0 is included in the update (array only, object is fine) no updates are performed on any fields

Example:

$updateObj = array(
    '1' => 200
);

Works, field 1 is updated.

$updateObj = array(
    '0' => 100,
    '1' => 200
);

Does not work, neither field 0 or 1 are updated.

I think I'm going to submit a bug report.


I've doing some investigations to why this happens. And I don't think I can find a way on how to "fix" this issue.

JavaScript has a difference between arrays and associative arrays/objects. PHP has the difference between arrays and objects. For PHP an associative array is an array, and for JavaScript it is an object.

When the PHP driver needs to convert an array to a JSON object, it tries to figure out whether an array is either: a normal array with sequentially numbered keys starting with 0; or an associative array. The current implementation regards any array with sequentially numbered keys, starting from 0 an normal array. And a normal array does not contain keys. And this is the problem. In the situation the driver sees a normal array, there is no field name information in the BSON that's send to the server, and hence the server can't update a field.

I can't think of a way to change this behaviour without breaking any sort of existing code. So if you want numerical fieldnames, you will have to use a stdClass object for the "main document". Alternatively, you could push those keys into a embedded document and then update:

<?php
$m = new Mongo;
$collection = $m->demo->testcollection;

$collection->insert(array(
    "_id" => 'bug341',
    'data' => array( 0, 1, 1, 2, 3, 5 )
));

$obj = $collection->findOne();

$update = array('data.0' => 'zero int');

$collection->update(
    array( '_id' => 'bug341' ),
    array( '$set' => $update )
);


$obj = $collection->findOne();
var_dump($obj);
?>

Tags:

Php

Mongodb