note 109868 modified in mongocollection.insert by danbrown

From: Date: Fri, 31 Aug 2012 15:37:43 +0000
Subject: note 109868 modified in mongocollection.insert by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190312@lists.php.net to get a copy of this message
Note, that the _id field will only be added to an inserted array if it does not already exist in the supplied array:

<?php
$data = array('x' => 12);
var_dump($data);
$collection->insert($data);
var_dump($data);
?>

Will output something like:

array(1) {
 ["x"]=>
 int(12)
}
array(2) {
 ["x"]=>
 int(12)
 ["_id"]=>
 object(MongoId)#196 (1) {
 ["$id"]=>
 string(24) "503e21fc0605290912000000"
 }
}

however,

$data = array('x' => 12, '_id' => NULL);
var_dump($data);
$collection->insert($data);
var_dump($data);

will not have the same result:

array(2) {
 ["x"]=>
 int(12)
 ["_id"]=>
 NULL
}
array(2) {
 ["x"]=>
 int(12)
 ["_id"]=>
 NULL
} --was-- Note, that the _id field will only be added to an inserted array if it does not already exist in the supplied array: $data = array('x' => 12); var_dump($data); $collection->insert($data); var_dump($data); Will output something like: array(1) { ["x"]=> int(12) } array(2) { ["x"]=> int(12) ["_id"]=> object(MongoId)#196 (1) { ["$id"]=> string(24) "503e21fc0605290912000000" } } however, $data = array('x' => 12, '_id' => NULL); var_dump($data); $collection->insert($data); var_dump($data); will not have the same result: array(2) { ["x"]=> int(12) ["_id"]=> NULL } array(2) { ["x"]=> int(12) ["_id"]=> NULL } http://php.net/manual/en/mongocollection.insert.php

« previous php.notes (#190312) next »