mongo Table类文件 获取MongoCursor(游标)的实现方法分析_PHP教程
MongoCursor Object
游标类
Mongo
Config.php配置文件
Table.php(mongodb操作数据库类文件)
Config.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::DEFAULT_HOST ;
private static $port = self::DEFAULT_PORT ;
private static $options = array(
'connect' => true,
'timeout' => 30,
//'replicaSet' => '' //If this is given, the master will 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(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数据库名称不正确');
}
}else{
throw new Zend_Exception('Mongodb数据库名称不能为空');
}
}
}
Table.php(mongodb操作数据库类文件)
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数据库名称不能为空');
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服务器连接失败');
}
}
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
/*
* 获取游标对象
*/
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;
}
}
要点注意!!!
第一种方法
//find cursor
/*
* 获取游标对象
*/
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 find($where=array(),$field=array()){
if($this->testLink()) {
$this->_data = $this->_db->find($where,$field)->sort(array("_id" => -1));
}
return $this;
}
/*
* 获取游标对象
*/
public function getCursor(){
return $this->_data;
}
第二种需要的是find得到的不是数组
find($where)->getCursor();是MongoCursor Object
注意注意
find()返回的是当前对象
toArray()方法是把当前对象转换为数组
getCursor()方法是把当前对象转换为MongoCursor Object(游标对象)

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

而後悔莫及、人們常常會因為一些原因不小心刪除某些聯絡人、微信作為一款廣泛使用的社群軟體。幫助用戶解決這個問題,本文將介紹如何透過簡單的方法找回被刪除的聯絡人。 1.了解微信聯絡人刪除機制這為我們找回被刪除的聯絡人提供了可能性、微信中的聯絡人刪除機制是將其從通訊錄中移除,但並未完全刪除。 2.使用微信內建「通訊錄恢復」功能微信提供了「通訊錄恢復」節省時間和精力,使用者可以透過此功能快速找回先前刪除的聯絡人,功能。 3.進入微信設定頁面點選右下角,開啟微信應用程式「我」再點選右上角設定圖示、進入設定頁面,,

番茄小說是一款非常熱門的小說閱讀軟體,我們在番茄小說中經常會有新的小說和漫畫可以去閱讀,每一本小說和漫畫都很有意思,很多小伙伴也想著要去寫小說來賺取賺取零用錢,在把自己想要寫的小說內容編輯成文字,那麼我們要怎麼樣在這裡面去寫小說呢?小伙伴們都不知道,那就讓我們一起到本站本站中花點時間來看寫小說的方法介紹。分享番茄小說寫小說方法教學 1、先在手機上打開番茄免費小說app,點擊個人中心——作家中心 2、跳到番茄作家助手頁面——點擊創建新書在小說的結

手機遊戲成為了人們生活中不可或缺的一部分,隨著科技的發展。它以其可愛的龍蛋形象和有趣的孵化過程吸引了眾多玩家的關注,而其中一款備受矚目的遊戲就是手機版龍蛋。幫助玩家們在遊戲中更好地培養和成長自己的小龍,本文將向大家介紹手機版龍蛋的孵化方法。 1.選擇合適的龍蛋種類玩家需要仔細選擇自己喜歡並且適合自己的龍蛋種類,根據遊戲中提供的不同種類的龍蛋屬性和能力。 2.提升孵化機的等級玩家需要透過完成任務和收集道具來提升孵化機的等級,孵化機的等級決定了孵化速度和孵化成功率。 3.收集孵化所需的資源玩家需要在遊戲中

華為手機如何實現雙微信登入?隨著社群媒體的興起,微信已成為人們日常生活中不可或缺的溝通工具之一。然而,許多人可能會遇到一個問題:在同一部手機上同時登入多個微信帳號。對於華為手機用戶來說,實現雙微信登入並不困難,本文將介紹華為手機如何實現雙微信登入的方法。首先,華為手機自帶的EMUI系統提供了一個很方便的功能-應用程式雙開。透過應用程式雙開功能,用戶可以在手機上同

字體大小的設定成為了重要的個人化需求,隨著手機成為人們日常生活的重要工具。以滿足不同使用者的需求、本文將介紹如何透過簡單的操作,提升手機使用體驗,調整手機字體大小。為什麼需要調整手機字體大小-調整字體大小可以使文字更清晰易讀-適合不同年齡段用戶的閱讀需求-方便視力不佳的用戶使用手機系統自帶字體大小設置功能-如何進入系統設置界面-在在設定介面中找到並進入"顯示"選項-找到"字體大小"選項並進行調整第三方應用調整字體大小-下載並安裝支援字體大小調整的應用程式-開啟應用程式並進入相關設定介面-根據個人

谷歌驗證器是一種用於保護使用者帳戶安全的工具,其金鑰是用於產生動態驗證碼的重要資訊。如果忘記了谷歌驗證器的金鑰,只能透過安全碼進行驗證,那麼下文站小編就會為大家帶來谷歌安全碼在哪裡取得的詳細內容介紹,希望能幫助到大家,想要了解的用戶們就請跟著下文繼閱讀吧!首先開啟手機設置,進入設定頁面。下拉頁面,找到Google。進入Google頁面,點選Google帳號。進入帳號頁面,點選驗證碼下方的檢視。輸入密碼或使用指紋驗證身分。取得Google安全碼,利用安全碼驗證Google身分。

在現今社會,手機已經成為我們生活中不可或缺的一部分。而微信作為我們日常溝通、工作、生活的重要工具,更是經常被使用。然而,在處理不同事務時可能需要分開兩個微信帳號,這就要求手機能夠支援同時登入兩個微信帳號。華為手機作為國內知名品牌,很多人使用,那麼華為手機開啟兩個微信帳號的方法是怎麼樣的呢?下面就來揭秘一下這個方法。首先,要在華為手機上同時使用兩個微信帳號,最簡

如何在華為手機上實現微信分身功能隨著社群軟體的普及和人們對隱私安全的日益重視,微信分身功能逐漸成為人們關注的焦點。微信分身功能可以幫助使用者在同一台手機上同時登入多個微信帳號,方便管理和使用。在華為手機上實現微信分身功能並不困難,只需要按照以下步驟操作即可。第一步:確保手機系統版本和微信版本符合要求首先,確保你的華為手機系統版本已更新至最新版本,以及微信App
