I'm experimenting with MongoDB using the PHP PECL extension, however I'm having difficulty getting a certain update query to work. I have searched around on SO for answers with little luck.
I have created a basic collection:
$m = new Mongo;
$collection = $m->testdb->testcollection;
$collection->insert(array(
0, 1, 1, 2, 3, 5
));
Using findOne
and var_dump
the record appears as follows:
array
'_id' =>
object(MongoId)[6]
public '$id' => string '4f3bde65a1f7a0315b000000' (length=24)
0 => int 0
1 => int 1
2 => int 1
3 => int 2
4 => int 3
5 => int 5
The problem comes when I want to update using $set
. I am basing my query on the mapping shown towards the bottom of the SQL to Mongo Cheat Sheet in the PHP manual
Here I want to update field 0
to value 100
$obj = $collection->findOne();
$collection->update(
array('_id' => $obj['_id']),
array('$set' => array(0 => 100))
);
Re-fetching the record shows that it remains unchanged.
I did wonder if I was doing something wrong with the _id
, however the following update query does work, albeit replacing the entire record with a new value, not simply updating the one field.
$collection->update(
array('_id' => $obj['_id']),
array(0 => 100)
);
Object dump:
array
'_id' =>
object(MongoId)[7]
public '$id' => string '4f3bde65a1f7a0315b000000' (length=24)
0 => int 100
Can someone please point out what I am doing wrong, and how to properly use $set
. I'm sure it's obvious and I just need a second pair of eyes on it.
Many thanks.
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);
?>
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.
You can't use a number as a valid field name in mongodb. Try putting your field "0" in quotes, which is how it's implemented in actuality.
we can update record in mongo db using php example below:-
$m = new MongoClient(<Put Replica set>); //PHP mongoclient function
echo "Connection to database successfully";
$db = $m->dbname; //put DB name
$collection = $db->profile;
$document = array(
"name" => $_REQUEST['name'],
"email" => $_REQUEST['email'],
"age" => $_REQUEST['age'],
"address" => $_REQUEST['address'],
"comment"=> $_REQUEST['comment']
);
// print_r($document); die;
//echo $_REQUEST['pfid'];
$filter=array('_id'=>new MongoID( $_REQUEST['pfid'] ));
$update=array('$set'=>$document);
$collection->update( $filter, $update);