One of the great things about Mongo is the ability to have arrays associated with a record without having to add an additional related table (as you do in a traditional database). In PHP, you can easily add an element to a Mongo database array with the $push command through either an update or an upsert.
To make an update (if the record already exists), use this command on your collection object:
$myCollection->update($queryArray, array('$push' => $newData ));To make it an upsert (update if present, insert if not), use this:
$upsert = true;
$myCollection->update($queryArray, array('$push' => $newData ), array("upsert" => $upsert));That will add the information in $newData to the array. So to add a timestamp entry to an array of timestamps where there is an "id" column, you could use this command:
$myCollection->update(array('id'=>1), array('$push' => array('ts'=>time()) ), array("upsert" => $upsert));The command will find the first record with an id of 1 and push the current timestamp into an array called"ts"and create it if it doesn't exist.



Del.ici.ous


