MONGO table class file gets the Mongocursor (cursor) method

Source: Internet
Author: User
Tags mongodb mongodb server testlink

Mongocursor Object

Cursor class

Mongo

config.php configuration file

table.php (MongoDB Operation database class file)

config.php configuration file

<?php require_once ' zend/exception.php ';
    Class Hrs_mongo_config {Const VERSION = ' 1.7.0 ';
    Const Default_host = ' localhost ';

    Const DEFAULT_PORT = 27017;
    private static $host = self::D efault_host;
    private static $port = self::D efault_port; private static $options = Array (' Connect ' => true, ' timeout ' =>,//' replicas

    Et ' => '//if This is given, the master'll be determined by using the IsMaster Database command on the seeds);
    public static $conn = ';
    public static $defaultDb = ';
    public static $linkStatus = '; public static function set ($server = ' mongodb://localhost:27017 ', $options = Array (' Connect ' => true)) {if (!$
        Server) {$url = ' mongodb://'. Self:: $host. ': '. Self:: $port;
            } if (Is_array ($server)) {if isset ($server [' Host ']) {self:: $host = $server [' Host '];
    if (Isset ($server [' Port '])} {            Self:: $port = $server [' Port ']; } if (Isset ($server [' user ']) && isset ($server [' Pass ']) {$url = ' mongodb://'. $server [' User '. $server [' Pass ']. '
            @ '. Self:: $host. ': '. Self:: $port;
            }else{$url = ' mongodb://'. Self:: $host. ': '. Self:: $port; } if (Is_array ($options)) {foreach (self:: $options as $o _k=> $o _v) {if (ISS
            ET ($options [$o _k]) Self:: $options [$o _k] = $o _v;
            } try{Self:: $conn = new Mongo ($url, Self:: $options);
        Self:: $linkStatus = ' success ';
        }catch (Exception $e) {self:: $linkStatus = ' failed ';
        } if (Isset ($server [' database ']) {self::selectdb ($server [' database ']); The public static function Selectdb ($database) {if ($database) {try {if (self: : $linkStatus== ' success ') Self:: $defaultDb = self:: $conn->selectdb ($database);
            Return self:: $defaultDb;
            catch (InvalidArgumentException $e) {throw new zend_exception (' MongoDB database name is incorrect ');
        }}else{throw new Zend_exception (' MongoDB database name cannot be empty '); }
    }
}



table.php (MongoDB Operation database class file)

<?php require_once ' hrs/mongo/config.php ';
    Abstract class Hrs_mongo_table {protected $_db = ';
    protected $_name = ';

    Protected $_data = Array ();
    protected $c _options = Array (' Fsync ' =>true, ' safe ' =>true);
            protected $u _options = Array (//' Upsert ' =>false, ' multiple ' =>true, ' Fsync ' =>true,
    ' Safe ' =>true);
            /* protected $r _options = Array (); * * protected $d _options = Array (' Fsync ' =>true,

    ' Justone ' =>false, ' safe ' =>true); protected function _setadapter ($database = ') {if (! $database) throw new Zend_exception (' MongoDB database name cannot be

        Empty ');
    Hrs_mongo_config::selectdb ($database); The Public Function __construct () {if (Hrs_mongo_config:: $conn instanceof Mongo) {$name = $this-&G
            T;_name;
            $defDb = Hrs_mongo_config:: $defaultDb; $this->_db= $defDb-> $name;
        }else{throw new Zend_exception (' MongoDB server Connection failed ');
        The Public Function inserts ($DATA) {if (! $this->testlink ()) return false;
        $ret = $this->_db->insert ($data, $this->c_options);
    return $ret;
        The Public Function update ($DATA, $where) {if (! $this->testlink ()) is return false;
    return $this->_db->update ($where, $data, $this->u_options);
                Public function Find ($where =array (), $limit =0) {if ($this->testlink ()) {if ($limit >0) { $this->_data = $where? $this->_db->find ($where)->limit ($limit)->snapshot (): $this->_db->find ()->limit ($limit)-
            >snapshot (); }else{$this->_data = $where? $this->_db->find ($where)->limit ($limit)->snapshot (): $this-
            >_db->find ()->limit ($limit)->snapshot ();
    } return $this; }//find CURsor/* Gets the cursor object/Public function look ($where =array (), $fields =array ()) {if ($this->testlink ( ) {if ($fields) {return $where? $this->_db->find ($where, $fields): $this->_db->fi
            nd ()->fields ($fields);
            }else{return $where? $this->_db->find ($where): $this->_db->find ();
    return false;
        The Public Function Delete ($where) {if (! $this->testlink ()) is return false;
    return $this->_db->remove ($where, $this->d_options);
        The Public Function Dropme () {if (! $this->testlink ()) is return false;
    return $this->_db->drop ();
    The Public Function __tostring () {return $this->_data;
        The Public Function ToArray () {$tmpData = array ();
            foreach ($this->_data as $id => $row) {$one _row = array (); foreach ($row as $key => $col) {$one _row[$key] = $col;
            $one _row[' _id '] = $id;
        $tmpData [] = $one _row;
    return $tmpData;
    } protected function Testlink () {return Hrs_mongo_config:: $linkStatus = = ' success '? True:false; }
}

Important Note ...

The first of these methods

    Find cursor
    /
     * Get Cursor object/public
    function look ($where =array (), $fields =array ()) {
        if ($this- >testlink ()) {
            if ($fields) {return
                $where? $this->_db->find ($where, $fields): $this->_db-> Find ()->fields ($fields);
            else{return
                $where? $this->_db->find ($where): $this->_db->find ();
            }
        return false;
    }

The second method

    Public function Find ($where =array (), $field =array ()) {
        if ($this->testlink ()) {
            $this->_data = $this- >_db->find ($where, $field)->sort (Array ("_id" =>-1));
        return $this;
    }
    
     * * Get Cursor object
    /Public Function getcursor () {return
    	$this->_data;
    }

The second need is find to get the not array

Find ($where)->getcursor (); Mongocursor Object

Pay attention.
Find () returns the current object

The ToArray () method converts the current object to an array

The GetCursor () method converts the current object to the Mongocursor object (Cursor objects)

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.