2016-03-25 14 views
2

user_notifications adlı bir veritabanında, her kullanıcı için kullanıcı kimliği olan bir koleksiyon vardır.Bir belgenin değerini güncelleme MongoDB PHP

Her koleksiyon için sayacı sayaç varsayılan ayarında 0 olacak şekilde ayarlanmış statik bir belge olmalıdır.

Basit bir yöntem çağrısında MongoDB'de yeni bir kullanıcı olarak sayaç değerini 1 artırmanız gerekiyor, sanırım berbat bir durumdayım. Bir sahte kod MongoDB bir acemi olarak :[

, benim şema tasarımı nasıl olduğu gibi bu yöntemi yazma sona erdi

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; 
      ] 
     ]); 
    } 
} 

: burada yapmak istiyorum benim nesnedir, bir fikir mi gerekiyor? daha iyi hale getirmek için herhangi bir öneri? Ayrıca, yukarıdaki sorguları nasıl çalıştırmalıyım?

Teşekkür

cevap

İlgili konular