PHP implementation of the MongoDB operation class, PHP implementation mongodb_php Tutorial

Source: Internet
Author: User

PHP implementation of the MongoDB operation class, PHP implementation MongoDB


mongo_db.php

<?php/** * Created by Phpstorm.  * User:yangyulong * DATE:2015/5/26 * time:13:45 */class mongo_db{private static $instanceof = NULL;  Public $mongo;  Private $host = ' localhost ';   Private $port = ' 27017 ';  Private $db;  Public $dbname;   Private $table = NULL;     /** * Initialize class, get MONGO instance object */Public function __construct ($host = null, $port = NULL, $dbname = NULL, $table = null) { if (NULL = = = $dbname) {$this->throwerror (' collection cannot be empty!    ');    }//Determines whether the host and port if (NULL!== $host) are passed {$this->host = $host;    } if (NULL!== $port) {$this->port = $port;     } $this->table = $table; $this->mongo = new Mongoclient ($this->host. ':' .    $this->port);      if ($this->getversion () >= ' 0.9.0 ') {$this->dbname = $this->mongo->selectdb ($dbname);    $this->db = $this->dbname->selectcollection ($table);    } else {$this->db = $this->mongo-> $dbname, $table; }} public Function GetVersion () {return mongoclient::version; }/** * Singleton mode * @return Mongo|null *///public static function getinstance ($host =null, $port =null, $dbname =null, $ Table=null) {////if (! (  Self:: $instanceof instanceof Self)} {//self:: $instanceof = new self ($host, $port, $dbname, $table);  }////Return self:: $instanceof; }/** * Insert a data * @param array $doc */Public Function Insert ($doc = Array ()) {if (empty ($doc)) {$thi S->throwerror (' inserted data cannot be empty!    ');      }//Save data information try {if (! $this->db->insert ($doc)) {throw new Mongoexception (' Insert data failed ');    }} catch (Mongoexception $e) {$this->throwerror ($e->getmessage ());      }}/** * Insert multiple Data messages * @param array $doc */Public Function insertmulti ($doc = Array ()) {if (empty ($doc)) { $this->throwerror (' inserted data cannot be empty!    '); }//Insert data information foreach ($doc as $key + $val) {//To determine if $val is an array if (Is_array ($val)) {$this->insert      ($val);  }    }/** * Find a record * @return Array|null */Public Function findOne ($where = null) {if (null = = = $where) {T        ry {if ($result = $this->db->findone ()) {return $result;        } else {throw new mongoexception (' Find data failed ');      }} catch (Mongoexception $e) {$this->throwerror ($e->getmessage ());        }} else {try {if ($result = $this->db->findone ($where)) {return $result;        } else {throw new mongoexception (' Find data failed ');      }} catch (Mongoexception $e) {$this->throwerror ($e->getmessage ()); }}}/** * Todo with conditional subsequent do * Find all Documents * @return Mongocursor */Public Function find ($where = NULL) {if ( NULL = = $where) {try {if ($result = $this->db->find ()) {} else {throw new Mongoex        Ception (' Find data failed ');      }} catch (Mongoexception $e) {$this->throwerror ($e->getmessage ()); }} ELSE {try {if ($result = $this->db->find ($where)) {} else {throw new mongoexception (        ' Find data failed ');      }} catch (Mongoexception $e) {$this->throwerror ($e->getmessage ());    }} $arr = Array ();    foreach ($result as $id = + $val) {$arr [] = $val;  } return $arr; }/** * Gets the number of record bars * @return int */Public Function GetCount () {try {if ($count = $this->db->count ()      ) {return $count;      } else {throw new mongoexception (' Total lookup failed ');    }} catch (Mongoexception $e) {$this->throwerror ($e->getmessage ());  }}/** * get all databases * @return Array */Public function Getdbs () {return $this->mongo->listdbs (); /** * Delete database * @param null $dbname * @return Mixed */Public function dropdb ($dbname = null) {if (null! =      = $dbname) {$retult = $this->mongo->dropdb ($dbname);      if ($retult [' OK ']) {return TRUE; } else {return FALSE;  }} $this->throwerror (' Please enter the name of the database to be deleted ');  /** * Force close link to database */Public Function closedb () {$this->mongo->close (TRUE); /** * Output error message * @param $errorInfo Error Contents */Public Function throwerror ($errorInfo = ') {echo '

Something went wrong: $errorInfo

"; Die (); } }

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/1008012.html www.bkjia.com true http://www.bkjia.com/PHPjc/1008012.html techarticle PHP Implementation of the MongoDB operation class, PHP implementation MongoDB mongo_db.php PHP/** * Created by Phpstorm. * User:yangyulong * DATE:2015/5/26 * Time: 13:45 */class mongo_db{private static $ins ...

  • 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.