note 112254 added to mongocollection.update
| From: | VasiliyMakogon | Date: | Thu, 23 May 2013 15:30:49 +0000 |
| Subject: | note 112254 added to mongocollection.update | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194888@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:
$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)
);
----
Server IP: 69.147.83.199
Probable Submitter: 91.220.181.35
----
Manual Page -- http://php.net/manual/en/mongocollection.update.php
Edit -- https://master.php.net/note/edit/112254
Del: integrated -- https://master.php.net/note/delete/112254/integrated
Del: useless -- https://master.php.net/note/delete/112254/useless
Del: bad code -- https://master.php.net/note/delete/112254/bad+code
Del: spam -- https://master.php.net/note/delete/112254/spam
Del: non-english -- https://master.php.net/note/delete/112254/non-english
Del: in docs -- https://master.php.net/note/delete/112254/in+docs
Del: other reasons-- https://master.php.net/note/delete/112254
Reject -- https://master.php.net/note/reject/112254
Search -- https://master.php.net/manage/user-notes.php