2
在一個名爲user_notifications的數據庫中,每個用戶都有一個用他們的用戶標識的集合。更新文檔的值MongoDB PHP
應該有一個靜態文檔,其中包含一個計數器列默認設置爲0爲每個集合。
在一個簡單的方法調用中,我需要將計數器值增加1,作爲MongoDB中的新手,我認爲我搞砸了。需要一些想法,這裏是我的目標,將做到這一點:
private function increment_notification()
{
// database => notification
// collection => _238
// document => unseen_counter
$unseen = $this->notification->_238->unseen_counter; // selecting a single document
// if the document didn't exists then create the document and
// set a default value 0 to the counter row.
if(!$unseen) {
// create a new document
$this->notification->insertOne([
'_id' => 'unseen_counter',
'counter' => '0'
]);
} else {
// we already have the document
// now update the counter value by 1
$unseen->update([
'$set' => [
'counter' => $unseen['counter'] + 1;
]
]);
}
}
我結束了寫這個方法的僞代碼:[
如MongoDB的初學者,怎麼會是我的架構設計?任何建議,使其更好? 另外,我應該如何運行上述查詢?
感謝
你能在這裏提供更多的細節嗎? – kvorobiev