thinkphp MONGO driver Update method supports Upsert parameters

Source: Internet
Author: User

MONGO database update operation has a key feature relative to MySQL, it can use Upsert mode, when the updated data does not exist, directly inserted, but thinkphp MONGO driver actually does not support this feature, no way, self-reliance.

thinkphp Driver layer, see because of the support of a variety of Db, but also using inheritance, the use of the class hierarchy is deep, responsible for the MONGO driver is DbMongo.class.php, the file is located in Thinkphp\extend\driver\db, Modifies the $options parameter of the Update method and, if the Update method provides the Upsert option, updates MongoDB in Upsert mode.

   1: Public  function Update ($data, $options) {
   2:          if (isset ($options [' table '])) { 
   3:              $this->switchcollection ($options [' table ']); 
   4:          }
   5:          $This->model  =   $options [' model ']; 
   6:          N (' Db_write ', 1);
   7:          $query   = $this->parsewhere ($options [' where ']); 
   8:          $set  =  $this->parseset ($data);
   9:          $isUpsert = $options [false;
  Ten:          if ($this->debug) { 
  One:              $this->querystr   =  $this->_dbname.'. '. $this->_collectionname. '. Update (';    
  :              $this->querystr   . = $query? Json_encode ($query):' {} '; 
  :              $this->querystr   . =  ', '. Json_encode ($set). ';  
  :          }
  :          try{
  :              //record start execution time
  :              G (' querystarttime ');
  :              $result   = $this->_collection->update ($query, $set, Array (true)); 
  :              $this->debug ();
  :              return $result;
  :          catch (mongocursorexception $e) {
  :              throw_exception ($e->getmessage ());
  :          }
  :      }

thinkphp MONGO driver Update method supports Upsert parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.