note 112254 modified in mongocollection.update by danbrown
| From: | danbrown@php.net | Date: | Wed, 05 Jun 2013 17:54:20 +0000 |
| Subject: | note 112254 modified in mongocollection.update by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-195078@lists.php.net to get a copy of this message | ||
if you want emulate on mongoDb "insert on duplicate key update" in SQL notation, you must
use flag "upsert" with "$set" in "update" operation.
In this sample uses unique key (user id and product id) and date user visit product:page. Each
execute this code make update 'lasttime' field:
<?php
$client = new \MongoClient(MONGO_DB);
$users = $client->users;
$last_views = $users->createCollection('last_views');
// create unique index
$result = $last_views->ensureIndex(
array("user_id" => 1, "warecode" => 1),
array("unique" => 1)
);
$uviews = new \stdClass();
$uviews->user_id = $this->user_id;
$uviews->warecode = $this->warecode;
$res = $users->last_views->update(
$uviews,
array('$set' => array('lasttime' => new \MongoTimestamp())),
array('upsert' => true)
);
?>
--was--
if you want emulate on mongoDb "insert on duplicate key update" in SQL notation, you must
use flag "upsert" with "$set" in "update" operation.
In this sample uses unique key (user id and product id) and date user visit product:page. Each
execute this code make update 'lasttime' field:
$client = new \MongoClient(MONGO_DB);
$users = $client->users;
$last_views = $users->createCollection('last_views');
// create unique index
$result = $last_views->ensureIndex(
array("user_id" => 1, "warecode" => 1),
array("unique" => 1)
);
$uviews = new \stdClass();
$uviews->user_id = $this->user_id;
$uviews->warecode = $this->warecode;
$res = $users->last_views->update(
$uviews,
array('$set' => array('lasttime' => new \MongoTimestamp())),
array('upsert' => true)
);
http://php.net/manual/en/mongocollection.update.php