Mongocursor Object
Cursor class
Mongo
config.php configuration file
table.php (MongoDB Operation database class file)
config.php configuration file
Copy Code code as follows:
<?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 ' => 30,
' Replicaset ' => '//if ' is given, the master would be determined by using the IsMaster Database command on the SE Eds
);
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 (Isset ($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 ']);
}
}
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)
Copy Code code as follows:
<?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);
}
Public Function __construct () {
if (hrs_mongo_config:: $conn instanceof Mongo) {
$name = $this->_name;
$defDb = Hrs_mongo_config:: $defaultDb;
$this->_db = $defDb-> $name;
}else{
throw new Zend_exception (' MongoDB server Connection failed ');
}
}
Public Function Insert ($data) {
if (! $this->testlink ()) return false;
$ret = $this->_db->insert ($data, $this->c_options);
return $ret;
}
Public Function Update ($DATA, $where) {
if (! $this->testlink ()) 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
/*
* 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;
}
Public Function Delete ($where) {
if (! $this->testlink ()) return false;
return $this->_db->remove ($where, $this->d_options);
}
Public Function Dropme () {
if (! $this->testlink ()) return false;
return $this->_db->drop ();
}
Public Function __tostring () {
return $this->_data;
}
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
Copy Code code as follows:
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
Copy Code code as follows:
Public function Find ($where =array (), $field =array ()) {
if ($this->testlink ()) {
$this->_data = $this->_db->find ($where, $field)->sort (Array ("_id" =>-1));
}
return $this;
}
Copy Code code as follows:
/*
* 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)