_php instance of MongoDB operation class implemented by PHP

Source: Internet
Author: User
Tags findone getmessage mongoclient 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;
  /** * Initializes the class, obtains the MONGO instance object/Public function __construct ($host = null, $port = NULL, $dbname = NULL, $table = NULL) {if (NULL = = $dbname) {$this->throwerror (' collection cannot be empty!
    ');
    }//Judge whether the host and port if (NULL!== $host) {$this->host = $host are passed;
    } 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;
  The Public Function getversion () {return mongoclient::version; /** * Single case mode * @return mongo|null///public static function getinstance ($host =null, $port =null, $dbname =n ull, $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)) { $this->throwerror (' inserted data cannot be empty!
    ');
      ///Save data information try {if (! $this->db->insert ($doc)) {throw new Mongoexception (' Insert data failed ');
    The catch (Mongoexception $e) {$this->throwerror ($e->getmessage ()); /** * Inserts multiple data information * @param array $doc/Public function insertmulti ($doc = Array ()) {if Empty ($ Doc) {$this->throwerror (' The inserted data cannot be empty!
    '); }//Insert data information foreach ($doc as $key => $val) {//To determine if $val is not an array if (Is_array ($val)) {$this->insert ($val); /** * Find a record * @return Array|null */Public Function findone ($where = null) {if (null = =
        = $where) {try {if ($result = $this->db->findone ()) {return $result;
        else {throw new mongoexception (' Find data failed ');
      The catch (Mongoexception $e) {$this->throwerror ($e->getmessage ());
        } else {try {if ($result = $this->db->findone ($where)) {return $result;
        else {throw new mongoexception (' Find data failed ');
      The catch (Mongoexception $e) {$this->throwerror ($e->getmessage ());
  /** * TODO with conditional subsequent * Find all documents * @return Mongocursor */Public Function found ($where = NULL)
     {if (NULL = = $where) {try {if ($result = $this->db->find ()) {} else {     throw new Mongoexception (' Find data failed ');
      The catch (Mongoexception $e) {$this->throwerror ($e->getmessage ()); } else {try {if ($result = $this->db->find ($where)) {} else {throw new M
        Ongoexception (' Find data failed ');
      The 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-&GT;DB-&G
      T;count ()) {return $count;
      else {throw new mongoexception (' Find total failed ');
    The catch (Mongoexception $e) {$this->throwerror ($e->getmessage ()); /** * Get all database * @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-&G
      T;mongo->dropdb ($dbname);
      if ($retult [' OK ']) {return TRUE;
      else {return FALSE;
  }} $this->throwerror (' Enter the name of the database to be deleted ');
  /** * Forces the link to close the database/Public function closedb () {$this->mongo->close (TRUE); /** * Output error message * @param $errorInfo error content/Public Function throwerror ($errorInfo = ') {echo "&LT;H3&G
    t; a mistake: $errorInfo  

The above is the entire contents of this article, I hope you can enjoy.

Related Article

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.