日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

MongoDB for PHP扩展操作类

發布時間:2024/9/20 php 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MongoDB for PHP扩展操作类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  • <?php??
  • ??
  • /**?
  • ?*???
  • *?@link????????https://github.com/thendfeel/TmongoDB?
  • *?@example???????
  • *?@copyright?????
  • *?@site????????http://www.uacool.com?
  • *?@created?????2013-12-13?
  • *?
  • *?Manual?
  • *?http://us2.php.net/mongo?
  • *?針對阿里云MongoDB數據庫?
  • *?SQL?to?Mongo?Mapping?Chart?
  • *?http://us2.php.net/manual/en/mongo.sqltomongo.php?
  • *?單例模式(只針對相同的config)?
  • */??
  • class?TmongoDB??
  • {??
  • ??
  • ????protected?$_db?=?'test';??
  • ??
  • ????protected?$_collection?=?'user';??
  • ??
  • ????protected?$_validate?=?array();??
  • ??
  • ????protected?static?$_mongoObj?=?array();??
  • ??????
  • ????private?$_exeResult?=?null;??
  • ????protected?$_sql?=?array();??
  • ??????
  • ????protected?$_mongo?=?null;???
  • ??????
  • ????const?CONNECT_DB?=?'admin';?//?連接數據庫,默認為admin??
  • ??????
  • ????/**?
  • ?????*?Config?For?MongDB?
  • ?????*?
  • ?????*?@var?array?
  • ?????*/??
  • ????protected?$_config?=?array(??
  • ????????????'mongo_seed1'?=>?'localhost',??
  • ????????????'mongo_seed2'?=>?'',??
  • ????????????'mongo_replname'?=>?'',??
  • ????????????'mongo_user'?=>?NULL,??
  • ????????????'mongo_pwd'?=>?NULL??
  • ????);??
  • ??
  • ????public?function?__construct($config,$db,?$collection)??
  • ????{??
  • ????????foreach?($this->_config?as?$k?=>?$v)?{??
  • ????????????if(isset($config[$k])){??
  • ????????????????$this->_config[$k]?=?$config[$k];??
  • ????????????}?else?{??
  • ????????????????E('mongoDB數據庫連接,請傳遞'.$k);??
  • ????????????}??
  • ????????}??
  • ??
  • ????????$this->_db?=?$db;??
  • ????????$this->_collection?=?$collection;??
  • ??????????
  • ????????$this->init();??
  • ????}??
  • ??????
  • ????public?function?getMongo()??
  • ????{??
  • ????????return?$this->_mongo;??
  • ????}??
  • ??????
  • ????/**?
  • ?????*?Init?The?Class?
  • ?????*?對于相同的配置只初始化一次?
  • ?????*?@param?string?$db?
  • ?????*?@param?string?$collection?
  • ?????*/??
  • ????public?function?init()??
  • ????{??
  • ????????$config?=?$this->_config;??
  • ????????ksort($config);??
  • ????????$encryptStr?=?'';??
  • ???????????
  • ????????foreach?($config?as?$k?=>?$v)?{??
  • ????????????$encryptStr?.=?($k.$v);??
  • ????????}??
  • ??????????
  • ????????$key?=?md5($encryptStr);??
  • ??????????
  • ????????if?(!self::$_mongoObj[$key])?{??
  • ????????????/*$conStr?=?"mongodb://";?
  • ?????????????
  • ????????????if?($config['user']?&&?$config['password'])?{?
  • ????????????????$conStr?.=?"{$config['user']}:{$config['password']}@";?
  • ????????????}?
  • ?????????????
  • ????????????$conStr?.=?"{$config['host']}:{$config['port']}";?
  • ?????????????
  • ????????????$_mongoDB?=?new?\Mongo($conStr,?array(?
  • ????????????????????"connect"?=>?false?
  • ????????????));*/??
  • ????????????$demo_uri?=?'mongodb://'?.?$config['mongo_user']?.?':'?.?$config['mongo_pwd']?.?'@'?.??
  • ????????????????????$config['mongo_seed1']?.?','?.?$config['mongo_seed2']?.?'/'?.?self::CONNECT_DB?.?'?replicaSet='?.?$config['mongo_replname'];??
  • ???????????
  • ????????????$manager?=?new?\MongoDB\Driver\Manager($demo_uri);??
  • ??????????????
  • ????????????self::$_mongoObj[$key]?=?$this->_mongo?=?$manager;??
  • ???????????????
  • ????????????/*if?($db?&&?$collection)?{?
  • ?????????????????
  • ????????????????$this->_mongoDB?=?$_mongoDB->selectCollection($db,?$collection);?
  • ????????????}?else?{?
  • ????????????????$this->_mongoDB?=?$_mongoDB->selectCollection($this->_db,$this->_collection);?
  • ????????????}*/??
  • ????????}?else?{???
  • ????????????$this->_mongo?=?self::$_mongoObj[$key];??
  • ????????}??
  • ????}??
  • ??
  • ????/**?
  • ?????*?Set?Db?&?Collection?
  • ?????*?重新設置數據庫和數據表?
  • ?????*?@param?string?$db?
  • ?????*?@param?string?$collection?
  • ?????*/??
  • ????public?function?setDb($db?=?NULL,?$collection?=?NULL)??
  • ????{??
  • ????????if?($db)?{??
  • ????????????$this->_db?=?$db;??
  • ????????????//$this->_mongoDB?=?NULL;??
  • ????????????//$this->_mongoDB?=?$this->_mongoObj->selectCollection($this->_db,$this->_collection);??
  • ????????}??
  • ??????????
  • ????????if?($collection)?{??
  • ????????????$this->_collection?=?$collection;??
  • ????????}??
  • ??????????
  • ????????return?$this;??
  • ????}??
  • ??
  • ????/**?
  • ?????*?Set?Collection?
  • ?????*?重新設置數據表?
  • ?????*?@param?string?$collection?
  • ?????*/??
  • ????public?function?setCollection($collection?=?NULL)??
  • ????{??
  • ????????if?($collection)?{??
  • ????????????$this->_collection?=?$collection;??
  • ????????????//$this->_mongoDB?=?NULL;??
  • ????????????//$this->_mongoDB?=?$this->_mongoObj->selectCollection($this->_db,$collection);??
  • ????????}??
  • ??????????
  • ????????return?$this;??
  • ????}??
  • ????/**?
  • ?????*?獲取指定條件下的集合里的數據數量,默認使用_id主鍵字段?
  • ?????*/??
  • ????public?function?count($argv?=?array(),$fields?=?'_id')??
  • ????{??
  • ????????$result?=?$this->find($argv,$fields);??
  • ??????????
  • ????????if?($result)?{??
  • ????????????return?count($result);??
  • ????????}?else?{??
  • ????????????return?0;??
  • ????????}??
  • ????}??
  • ????/**?
  • ?????*?Fetch?From?Mongodb?
  • ?????*?
  • ?????*?@param?array?$argv?
  • ?????*?@param?number?$skip?
  • ?????*?@param?number?$limit?
  • ?????*?@param?array?$sort?
  • ?????*?@return?Ambigous?<multitype:,?multitype:>|boolean?
  • ?????*/??
  • ????public?function?find($argv?=?array(),$fields?=?array(),$sort?=?array(),$skip?=?0,?$limit?=?0)??
  • ????{??
  • ????????/*$argv?=?$this->validate($argv);?
  • ?????????
  • ????????if?($argv)?{?
  • ????????????$result?=?$this->_mongoDB->find($argv)?
  • ????????????->skip($skip)?
  • ????????????->limit($limit)?
  • ????????????->sort($sort);?
  • ????????????return?self::toArray($result);?
  • ????????}*/??
  • ??????????
  • ????????$options?=?array();??
  • ??????????
  • ????????if?($skip)?{??
  • ????????????$options['skip']?=?$skip;??
  • ????????}??
  • ??????????
  • ????????if?($limit)?{??
  • ????????????$options['limit']?=?$limit;??
  • ????????}??
  • ??????????
  • ????????if?($sort)?{??
  • ????????????$options['sort']?=?$sort;??
  • ????????}??
  • ??????????
  • ????????if?($fields)?{??
  • ????????????if?(is_string($fields))?{??
  • ????????????????$fields?=?explode(',',?$fields);??
  • ????????????}??
  • ??????????????
  • ????????????foreach?($fields?as?$v)?{??
  • ????????????????$options['projection'][$v]?=?1;??
  • ????????????}??
  • ????????}???
  • ??????????
  • ????????$query?=?new?\MongoDB\Driver\Query($argv,?$options);???
  • ??????????
  • ????????$cursor?=?$this->_mongo->executeQuery($this->_db.'.'.$this->_collection,?$query);??
  • ??????
  • ????????return?$cursor->toArray();??
  • ????}??
  • ????/**?
  • ?????*?執行命令?
  • ?????*/??
  • ????public?function?runCommand($command?=?array())??
  • ????{??
  • ????????if?(!$command)?{??
  • ????????????return?false;??
  • ????????}??
  • ??????????
  • ????????$commandObj?=?new?\MongoDB\Driver\Command($command);??
  • ??????????
  • ????????try?{??
  • ????????????$cursor?=?$this->_mongo->executeCommand($this->_db,?$commandObj);??
  • ????????????$response?=?$cursor->toArray();??
  • ????????}?catch(\MongoDB\Driver\Exception?$e)?{??
  • ????????????echo?'Mongo的runCommand異常:',$e->getMessage();??
  • ????????????exit;??
  • ????????}??
  • ??????????
  • ????????if?(count($response)?>?1)?{??
  • ????????????return?$response;??
  • ????????}?else?{??
  • ????????????return?$response[0];??
  • ????????}??
  • ????}??
  • ??????
  • ????/**?
  • ?????*?Fetch?By?MongoId?
  • ?????*?
  • ?????*?@param?string?$_id?
  • ?????*?@return?Ambigous?<Ambigous,?boolean,?multitype:>?
  • ?????*/??
  • ????public?function?findById($_id?=?'',$fields?=?array())??
  • ????{??
  • ????????if?(is_string($_id))?{??
  • ????????????return?$this->findOne(array('_id'?=>?new?\MongoDB\BSON\ObjectID($_id)),$fields);??
  • ????????}??
  • ????}??
  • ??
  • ????/**?
  • ?????*?Fetch?One?From?MongoDB?
  • ?????*?
  • ?????*?@param?array?$argv?
  • ?????*?@param?array?$fields?
  • ?????*?@return?multitype:?boolean?
  • ?????*/??
  • ????public?function?findOne($argv?=?array(),$fields?=?array(),$sort?=?array())??
  • ????{??
  • ????????$result?=?$this->find($argv,$fields,$sort,0,1);??
  • ??????????
  • ????????if?($result)?{??
  • ????????????return?$result[0];??
  • ????????}?else?{??
  • ????????????return?NULL;??
  • ????????}??
  • ????????????//return?$this->cleanId($this->_mongoDB->findOne($argv,?$fields));??
  • ????}??
  • ??
  • ????/**?
  • ????*?Update?MongoDB?By?Id?
  • ????*?通過主鍵ID更新?
  • ????*?@param?string?$_id?
  • ????*?@param?array?$newData?
  • ????*/??
  • ????public?function?updateById($_id,?$set?=?array())??
  • ????{??
  • ????????return?$this->updateStatement(array('_id'?=>?new?\MongoDB\BSON\ObjectID($_id)),?array('$set'=>$set))->execute()->getModifiedCount();??
  • ????}??
  • ??
  • ????/**?
  • ????*?執行添加,刪除,更新?All?From?Mongodb,執行多個語句?
  • ????*?$obj->deleteStatement(array('name'=>'1'))->deleteStatement(array('id'=>1))->remove();?
  • ????*?@param?array?$argv?
  • ????*/??
  • ????public?function?execute()??
  • ????{??
  • ????????if?(!$this->_sql)?{??
  • ????????????return?NULL;??
  • ????????}??
  • ??????????
  • ????????$bulk?=?new?\MongoDB\Driver\BulkWrite;??
  • ??????????
  • ????????foreach?($this->_sql?as?$val)?{??
  • ????????????switch?($val['type'])?{??
  • ????????????????case?'delete':??
  • ????????????????????$bulk->delete($val['sql'],$val['limit']);??
  • ????????????????????break;??
  • ????????????????case?'insert':??
  • ????????????????????$bulk->insert($val['document']);??
  • ????????????????????break;??
  • ????????????????case?'update':??
  • ????????????????????$bulk->update($val['filter'],$val['set'],$val['options']);??
  • ????????????????????break;??
  • ????????????}??
  • ??????????????
  • ????????}??
  • ??????????
  • ????????$writeConcern?=?new?\MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY,?1000);??
  • ??????????
  • ????????try?{??
  • ????????????$this->_exeResult?=?$this->_mongo->executeBulkWrite($this->_db.'.'.$this->_collection,?$bulk,?$writeConcern);??
  • ????????}?catch(\MongoDB\Driver\Exception\WriteException?$e)?{??
  • ????????????echo?'MongoDB擴展寫入異常:';??
  • ??????????????
  • ????????????$writeResult?=?$e->getWriteResult();??
  • ??????????????
  • ????????????if?($writeConcernError?=?$writeResult->getWriteConcernError())?{????
  • ????????????????echo?$writeConcernError[0]->getMessage(),'<br?/>';??
  • ????????????}??
  • ??????????????
  • ????????????if?($writeErrors?=?$writeResult->getWriteErrors())?{???
  • ????????????????echo?$writeErrors[0]->getMessage();??
  • ????????????}??
  • ????????????exit();??
  • ????????}?catch?(\MongoDB\Driver\Exception\InvalidArgumentException?$e)?{??
  • ????????????exit('MongoDB擴展傳入參數異常:'.$e->getMessage());??
  • ????????}?catch?(\MongoDB\Driver\Exception\RuntimeException?$e)?{??
  • ????????????exit('MongoDB擴展運行異常:'.$e->getMessage());??
  • ????????}?catch?(\MongoDB\Driver\Exception\ExecutionTimeoutException?$e)?{??
  • ????????????exit('MongoDB擴展運行超時異常:'.$e->getMessage());??
  • ????????}?catch?(\MongoDB\Driver\Exception\ConnectionTimeoutException?$e)?{??
  • ????????????exit('MongoDB擴展連接超時異常:'.$e->getMessage());??
  • ????????}?catch?(\Exception?$e)?{??
  • ????????????exit('系統異常:'.$e->getMessage());??
  • ????????}??
  • ??????????
  • ????????return?$this;??
  • ????}??
  • ????/**?
  • ?????*?獲取刪除的行數?
  • ?????*/??
  • ????public?function?getDeletedCount()??
  • ????{??
  • ????????if?($this->_exeResult)?{??
  • ????????????return?$this->_exeResult->getDeletedCount();??
  • ????????}?else?{??
  • ????????????return?0;??
  • ????????}??
  • ????}??
  • ????/**?
  • ?????*?獲取實際更新的行數?
  • ?????*/??
  • ????public?function?getModifiedCount()??
  • ????{??
  • ????????if?($this->_exeResult)?{??
  • ????????????return?$this->_exeResult->getModifiedCount();??
  • ????????}?else?{??
  • ????????????return?0;??
  • ????????}??
  • ????}??
  • ????/**?
  • ?????*?一次最多插入9萬條以下.耗時?
  • ?????*?獲取實際插入的行數?
  • ?????*/??
  • ????public?function?getInsertedCount()??
  • ????{??
  • ????????if?($this->_exeResult)?{??
  • ????????????return?$this->_exeResult->getInsertedCount();??
  • ????????}?else?{??
  • ????????????return?0;??
  • ????????}??
  • ????}??
  • ????/**?
  • ?????*?獲取實際匹配的行數?
  • ?????*/??
  • ????public?function?getMatchedCount()??
  • ????{??
  • ????????if?($this->_exeResult)?{??
  • ????????????return?$this->_exeResult->getMatchedCount();??
  • ????????}?else?{??
  • ????????????return?0;??
  • ????????}??
  • ????}??
  • ????/**?
  • ?????*?獲取實際更新失敗然后新插入的行數?
  • ?????*??
  • ?????*/??
  • ????public?function?getUpsertedCount()??
  • ????{??
  • ????????if?($this->_exeResult)?{??
  • ????????????return?$this->_exeResult->getUpsertedCount();??
  • ????????}?else?{??
  • ????????????return?0;??
  • ????????}??
  • ????}??
  • ????/**?
  • ?????*?獲取實際更新失敗然后新插入的ID列表?
  • ?????*/??
  • ????public?function?getUpsertedIds()??
  • ????{??
  • ????????if?($this->_exeResult)?{??
  • ????????????return?$this->_exeResult->getUpsertedIds();??
  • ????????}?else?{??
  • ????????????return?0;??
  • ????????}??
  • ????}??
  • ???????
  • ??????
  • ????/**?
  • ?????*?delete子句?
  • ?????*?@param?$delete?為刪除過濾條件,為數組形式?
  • ?????*/??
  • ????public?function?deleteStatement($delete,$limit?=?0)??
  • ????{??
  • ????????$this->_sql[]?=?array('type'=>'delete','sql'=>$delete,'limit'=>array('limit'=>$limit));??
  • ??????????
  • ????????return?$this;??
  • ????}??
  • ????/**?
  • ?????*?insert子句?
  • ?????*?@param?$batch?批量插入數據?
  • ?????*/??
  • ????public?function?insertStatement($insert,$batch?=?false)??
  • ????{??
  • ????????if?($batch)?{??
  • ????????????if?(is_array($insert)?&&?$insert)?{??
  • ????????????????foreach?($insert?as?$val)?{??
  • ????????????????????$this->_sql[]?=?array('type'=>'insert','document'=>$val);???
  • ????????????????}??
  • ????????????}??
  • ????????}?else?{??
  • ????????????$this->_sql[]?=?array('type'=>'insert','document'=>$insert);??
  • ????????}??
  • ??????????
  • ????????return?$this;??
  • ????}??
  • ????/**?
  • ?????*?update子句?
  • ?????*?@param?option?multi?為true則更新全部符合條件的文檔,否則只更新一個符合條件的文檔?
  • ?????*??????????????upsert?為true則當沒有符合條件的文檔時將更新過后的數據插入到集合中?
  • ?????*?參考連接:http://blog.csdn.net/qq1355541448/article/details/9082225?
  • ?????*?第二個參數有以下的做法:?
  • ?????*??修改更新???
  • ?????*??????使用set關鍵字:?$set:讓某節點等于給定值?,字段不變,內容變了?
  • ?????*??替換更新:?
  • ?????*??????第一個參數$where=array(‘column_name’=>’col709′),第二個參數:$newdata=array(‘column_exp’=>’HHHHHHHHH’,'column_fid’=>123);?
  • ?????*??????那么指定的column_name字段將會替換成成column_exp(=HHHHHHHHH)和column_fid(123)?
  • ?????*??自動累加或自動累減?
  • ?????*??????array(‘$set’=>$newdata,’$inc’=>array(’91u’=>-5),第二個參數,在找到的91u字段的參數會自動在原值減5?
  • ?????*??刪除指定字段?
  • ?????*??????$where=array(‘column_name’=>’col685′);?
  • ?????*??????$result=$collection->update($where,array(‘$unset’=>’column_exp’));column_exp字段將會被刪除?
  • ?????*?參考文檔:https://docs.mongodb.org/manual/reference/operator/update/?
  • ?????*/??
  • ????public?function?updateStatement($filter,$set,$options?=?array('multi'?=>?true,?'upsert'?=>?false))??
  • ????{??
  • ????????$this->_sql[]?=?array('type'=>'update','filter'=>$filter,'set'=>$set,'options'=>$options);??
  • ??????????
  • ????????return?$this;??
  • ????}??
  • ????/**?
  • ????*?Remove?By?Id?From?Mongodb?
  • ????*?
  • ????*?@param?string?$_id?
  • ????*?@return?Ambigous?<boolean,?multitype:>?
  • ????*/??
  • ????public?function?removeById($_id)??
  • ????{??
  • ????????return?$this->deleteStatement(array('_id'?=>?new?\MongoDB\BSON\ObjectID($_id)))->execute()->getDeletedCount();??
  • ????}??
  • ??
  • ????/**?
  • ????*?Remove?One?From?Mongodb?
  • ????*?
  • ????*?@param?array?$argv?
  • ????*/??
  • ????public?function?removeOne($argv?=?array())??
  • ????{??
  • ????????return?$this->deleteStatement($argv,1)->execute()->getDeletedCount();??
  • ????}??
  • ??
  • ?????/**?
  • ?????*?Remove?Field?From?MongoDB?
  • ?????*?
  • ?????*?@param?string?$_id?
  • ?????*?@param?array?$field?
  • ?????*/??
  • //??public?function?removeFieldById($_id,?$field?=?array())??
  • //??{??
  • //??????return?$this->updateStatement(array('_id'?=>?new?\MongoDB\BSON\ObjectID($_id)),?array('$unset'?=>?$unSetfield));??
  • //??}??
  • ??
  • ????/**?
  • ????*?Validate?Data?Callbak?Function?沒有用到的函數?
  • ????*?
  • ????*?@param?array?$argv?
  • ?????*/??
  • ????private?function?validate($data)??
  • ????{??
  • ????????if?($this->_validate)?{??
  • ????????????foreach?($this->_validate?as?$arg?=>?$validate)?{??
  • ????????????????if?(is_array($data)?&&?array_key_exists(strval($arg),?$data))?{??
  • ????????????????????foreach?($validate?as?$key?=>?$value)?{??
  • ????????????????????????switch?(strtolower($key))?{??
  • ????????????????????????????case?'type':??
  • ????????????????????????????????if?($value?==?'int')?{??
  • ????????????????????????????????????$data[$arg]?=?(int)?$data[$arg];??
  • ????????????????????????????????}?elseif?($value?==?'string')?{??
  • ????????????????????????????????????$data[$arg]?=?(string)?$data[$arg];??
  • ????????????????????????????????}?elseif?($value?==?'bool')?{??
  • ????????????????????????????????????$data[$arg]?=?(bool)?$data[$arg];??
  • ????????????????????????????????}?elseif?($value?==?'float')?{??
  • ????????????????????????????????????$data[$arg]?=?(float)?$data[$arg];??
  • ????????????????????????????????}?elseif?($value?==?'array')?{??
  • ????????????????????????????????????$data[$arg]?=?(array)?$data[$arg];??
  • ????????????????????????????????}??
  • ????????????????????????????????break;??
  • ????????????????????????????case?'min':??
  • ????????????????????????????????if?(strlen($data[$arg])?<?$value)?{??
  • ????????????????????????????????????exit('Error:?The?length?of?'?.?$arg?.?'?is?not?matched');??
  • ????????????????????????????????}??
  • ????????????????????????????????break;??
  • ????????????????????????????case?'max':??
  • ????????????????????????????????if?(strlen($data[$arg])?>?$value)?{??
  • ????????????????????????????????????exit('Error:?The?length?of?'?.?$arg?.?'?is?not?matched');??
  • ????????????????????????????????}??
  • ????????????????????????????????break;??
  • ????????????????????????????case?'func':??
  • ????????????????????????????????$call?=?preg_split('/[\:]+|\-\>/i',?$value);??
  • ????????????????????????????????if?(count($call)?==?1)?{??
  • ????????????????????????????????????$data[$arg]?=?call_user_func($call['0'],?$data[$arg]);??
  • ????????????????????????????????}?else?{??
  • ????????????????????????????????????$data[$arg]?=?call_user_func_array(array($call['0'],$call['1']),?array($data[$arg]));??
  • ????????????????????????????????}??
  • ????????????????????????????????break;??
  • ????????????????????????}??
  • ????????????????????}??
  • ????????????????}??
  • ????????????}??
  • ????????}??
  • ??????????????????????????????
  • ????????return?$data;??
  • ????}??
  • ????/**?
  • ?????*?mongdodb服務器的相關信息?
  • ?????*/??
  • ????public?function?buildInfo()??
  • ????{??
  • ????????return?$this->runCommand(array('buildinfo'=>1));??
  • ????}??
  • ????/**?
  • ?????*?返回指定集合的統計信息,包括數據大小、已分配的存儲空間和索引的大小。?
  • ?????*/??
  • ????public?function?collStats($collection?=?'')??
  • ????{??
  • ????????if?(!$collection)?{??
  • ????????????$collection?=?$this->_collection;??
  • ??????????????
  • ????????????if?(!$collection)?{??
  • ????????????????return?NULL;??
  • ????????????}??
  • ????????}??
  • ??????????
  • ????????return?$this->runCommand(array('collstats'=>$collection));??
  • ????}??
  • ????/**?
  • ?????*?列出指定集合中滿足查詢條件的文檔的指定鍵的所有不同值?
  • ?????*/??
  • ????public?function?distinct($field,$filter?=?array(),$collection?=?'')??
  • ????{??
  • ????????if?(!$collection)?{??
  • ????????????$collection?=?$this->_collection;??
  • ??????????????
  • ????????????if?(!$collection)?{??
  • ????????????????return?NULL;??
  • ????????????}??
  • ????????}??
  • ????????return?false;??
  • ????????return?$this->runCommand(array('key'=>$field,'query'=>$filter,'distinct'=>$collection));??
  • ????}??
  • ????/**?
  • ?????*?刪除集合的所有數據?
  • ?????*/??
  • ????public?function?drop($collection?=?'')??
  • ????{??
  • ????????if?(!$collection)?{??
  • ????????????$collection?=?$this->_collection;??
  • ??????????????
  • ????????????if?(!$collection)?{??
  • ????????????????return?NULL;??
  • ????????????}??
  • ????????}??
  • ??????????
  • ????????return?$this->runCommand(array('drop'=>$collection));??
  • ????}??
  • ????/**?
  • ?????*?刪除當前數據庫中的所有數據?
  • ?????*/??
  • ????public?function?dropDatabase()??
  • ????{??
  • ????????return?$this->runCommand(array('dropdatabase'=>1));??
  • ????}??
  • ????/**?
  • ?????*?刪除集合里面名稱為name的索引,如果名稱為"*",則刪除全部索引。?
  • ?????*/??
  • ????public?function?dropIndexes($index?=?'*',$collection?=?'')??
  • ????{??
  • ????????if?(!$collection)?{??
  • ????????????$collection?=?$this->_collection;??
  • ??????????????
  • ????????????if?(!$collection)?{??
  • ????????????????return?NULL;??
  • ????????????}??
  • ????????}??
  • ??????????
  • ????????return?$this->runCommand(array('dropIndexes'=>$collection,'index'?=>?$index));??
  • ????}??
  • //??/**??
  • //???*?創建索引??
  • //???*/??
  • //??public?function?createIndexes($index,$collection?=?'')??
  • //??{??
  • //??????if?(!$collection)?{??
  • //??????????$collection?=?$this->_collection;??
  • ??????????????????
  • //??????????if?(!$collection)?{??
  • //??????????????return?NULL;??
  • //??????????}??
  • //??????}??
  • ??????????
  • //??????return?$this->runCommand(array('createIndexes'=>array('create'=>$collection),'index'?=>?array('name'=>$index)));??
  • //??}??
  • ????/**?
  • ?????*?列出某個集合下所有的索引?
  • ?????*/??
  • ????public?function?listIndexes($collection?=?'')??
  • ????{??
  • ????????if?(!$collection)?{??
  • ????????????$collection?=?$this->_collection;??
  • ??????????
  • ????????????if?(!$collection)?{??
  • ????????????????return?NULL;??
  • ????????????}??
  • ????????}??
  • ??????????
  • ????????return?$this->runCommand(array('listIndexes'=>$collection));??
  • ????}??
  • ????/**?
  • ?????*?查找并修改?
  • ?????*/??
  • ????public?function?findAndModify($update?=?array(),$filter?=?array(),$collection?=?'')??
  • ????{??
  • ????????if?(!$collection)?{??
  • ????????????$collection?=?$this->_collection;??
  • ??????????????
  • ????????????if?(!$collection)?{??
  • ????????????????return?NULL;??
  • ????????????}??
  • ????????}??
  • ??????????
  • ????????return?$this->runCommand(array('findAndModify'=>$collection,'query'?=>?$filter,'update'=>$update));??
  • ????}??
  • ????/**?
  • ?????*?查看對本集合執行的最后一次操作的錯誤信息或者其它狀態信息。在w臺服務器復制集合的最后操作之前,這個命令會阻塞?
  • ?????*/??
  • ????public?function?getLastError()??
  • ????{??
  • ????????return?$this->runCommand(array('getLastError'=>1));??
  • ????}??
  • ????/**?
  • ?????*?檢查本服務器是主服務器還是從服務器?
  • ?????*/??
  • ????public?function?isMaster()??
  • ????{??
  • ????????return?$this->runCommand(array('ismaster'=>1));??
  • ????}??
  • ????/**?
  • ?????*?返回所有可以在服務器上運行的命令及相關信息。?
  • ?????*/??
  • ????public?function?listCommands()??
  • ????{??
  • ????????return?$this->runCommand(array('listCommands'=>1));??
  • ????}??
  • ????/**?
  • ?????*?管理專用命令,列出服務器上所有的數據庫?
  • ?????*/??
  • ????public?function?listDatabases()??
  • ????{??
  • ????????return?$this->setDb('admin')->runCommand(array('listDatabases'=>1));??
  • ????}??
  • ????/**?
  • ?????*?檢查服務器鏈接是否正常。即便服務器上鎖了,這條命令也會立刻返回?
  • ?????*/??
  • ????public?function?ping()??
  • ????{??
  • ????????return?$this->runCommand(array('ping'=>1));??
  • ????}??
  • ????/**?
  • ?????*?將集合a重命名為b,其中a和b都必須是完整的集合命名空間(例如"test.foo"代表test數據庫中的foo集合)?
  • ?????*?@param?dropTarget?Optional.?If?true,?mongod?will?drop?the?target?of?renameCollection?prior?to?renaming?the?collection.?The?default?value?is?false.?
  • ?????*?用法.?$fromCollection?=?'test.demo'?,?$toCollection?=?'test.demo1'?,一定要加數據庫前綴?
  • ?????*/??
  • ????public?function?renameCollection($fromCollection,$toCollection,$dropTarget?=?false)??
  • ????{??
  • ????????if?(!$fromCollection?||?!$toCollection)?{??
  • ????????????return?false;??
  • ????????}??
  • ??????????
  • ????????return?$this->setDb('admin')->runCommand(array('renameCollection'=>$fromCollection,'to'=>$toCollection,'dropTarget'=>$dropTarget));??
  • ????}??
  • ????/**?
  • ?????*?修復并壓縮當前數據庫,這個操作可能非常耗時。?
  • ?????*/??
  • ????public?function?repairDatabase()??
  • ????{??
  • ????????return?$this->setDb('admin')->runCommand(array('repairdatabase'=>1));??
  • ????}??
  • ????/**?
  • ?????*?返回這臺服務器的管理統計信息。?
  • ?????*/??
  • ????public?function?serverStatus()??
  • ????{??
  • ????????return?$this->runCommand(array('serverStatus'=>1));??
  • ????}??
  • ????/**?
  • ?????*?創建集合?
  • ?????*?$options?=?array('autoIndexId','capped','size','max','flags');?
  • ?????*/??
  • ????public?function?createCollection($collection,$options?=?array())??
  • ????{??
  • ????????$options['create']?=?$collection;??
  • ??????????
  • ????????return?$this->runCommand($options);??
  • ????}??
  • ????/**?
  • ?????*?刪除集合?
  • ?????*/??
  • ????public?function?dropCollection($collection)??
  • ????{??
  • ????????if?(!$collection)?{??
  • ????????????return?NULL;??
  • ????????}??
  • ??????????
  • ????????return?$this->runCommand(array('drop'=>$collection));??
  • ????}??
  • }??

  • 部分用法如下:

    [php] view plain copy
  • $config?=?array(??
  • ????????????????'mongo_seed1'?=>?C('mongo_seed1'),??
  • ????????????????'mongo_seed2'?=>?C('mongo_seed2'),??
  • ????????????????'mongo_replname'?=>?C('mongo_replname'),??
  • ????????????????'mongo_user'?=>?C('mongo_user'),??
  • ????????????????'mongo_pwd'?=>?C('mongo_pwd')??
  • ????????);??
  • ???????????
  • ????????$mongoObj?=?new?\Org\Util\TmongoDB($config,'demo','test');??
  • ??????????
  • ????????$insert?=?array(??
  • ????????????????array(??
  • ????????????????????????'name'=>?'走令敏',??
  • ????????????????????????'age'=>?24,??
  • ????????????????????????'address'?=>?'江西省贛州市',??
  • ????????????????????????'gender'?=>?'男',??
  • ????????????????????????'salary'?=>?5500??
  • ????????????????),??
  • ????????????????array(??
  • ????????????????????????'name'=>?'皇兄文',??
  • ????????????????????????'age'=>?24,??
  • ????????????????????????'address'?=>?'江西省撫州市',??
  • ????????????????????????'gender'?=>?'男',??
  • ????????????????????????'salary'?=>?6000??
  • ????????????????),??
  • ????????????????array(??
  • ????????????????????????'name'=>?'周李菲',??
  • ????????????????????????'age'=>?23,??
  • ????????????????????????'address'?=>?'江西省上饒市',??
  • ????????????????????????'gender'?=>?'男',??
  • ????????????????????????'salary'?=>?5000??
  • ????????????????),??
  • ????????????????array(??
  • ????????????????????????'name'=>?'主力科',??
  • ????????????????????????'age'=>?25,??
  • ????????????????????????'address'?=>?'江西省上饒市',??
  • ????????????????????????'gender'?=>?'男',??
  • ????????????????????????'salary'?=>?1000??
  • ????????????????),??
  • ????????????????array(??
  • ????????????????????????'name'=>?'夜舟',??
  • ????????????????????????'age'=>?25,??
  • ????????????????????????'address'?=>?'江西省上饒市',??
  • ????????????????????????'gender'?=>?'男',??
  • ????????????????????????'salary'?=>?7000??
  • ????????????????),??
  • ????????????????array(??
  • ????????????????????????'name'=>?'周倩倩',??
  • ????????????????????????'age'=>?23,??
  • ????????????????????????'address'?=>?'江西省上饒市',??
  • ????????????????????????'gender'?=>?'女',??
  • ????????????????????????'salary'?=>?7000??
  • ????????????????),??
  • ????????????????array(??
  • ????????????????????????'name'=>?'網點保',??
  • ????????????????????????'age'=>?22,??
  • ????????????????????????'address'?=>?'河南省鶴壁市',??
  • ????????????????????????'gender'?=>?'男',??
  • ????????????????????????'salary'?=>?10000??
  • ????????????????),??
  • ????????????????array(??
  • ????????????????????????'name'=>?'舒玉婷',??
  • ????????????????????????'age'=>?24,??
  • ????????????????????????'address'?=>?'江西省上饒市',??
  • ????????????????????????'gender'?=>?'女',??
  • ????????????????????????'salary'?=>?6000??
  • ????????????????),??
  • ????????????????array(??
  • ????????????????????????'name'=>?'少見波',??
  • ????????????????????????'age'=>?27,??
  • ????????????????????????'address'?=>?'遼寧省吉林市',??
  • ????????????????????????'gender'?=>?'男',??
  • ????????????????????????'salary'?=>?20000??
  • ????????????????),??
  • ????????????????array(??
  • ????????????????????????'name'=>?'李存平',??
  • ????????????????????????'age'=>?25,??
  • ????????????????????????'address'?=>?'江西省吉安市',??
  • ????????????????????????'gender'?=>?'男',??
  • ????????????????????????'salary'?=>?6000??
  • ????????????????)??
  • ????????);??
  • ??
  • ????????$arr?=?array();??
  • ??
  • ????????for?($i?=?0;$i?<?10;++$i)?{??
  • ????????????for?($j?=?0;$j?<?10;++$j)?{??
  • ????????????????$key?=?$i*10?+?$j;??
  • ????????????????$arr[$key]?=?$insert[$j];??
  • ????????????????$arr[$key]['num']?=?$key;??
  • ????????????????$arr[$key]['_id']?=?new?\MongoDB\BSon\ObjectID;??
  • ????????????}??
  • ????????}??
  • ???????????
  • ????????$starttime?=?microtime(true);??
  • ????????$result?=?$mongoObj->insertStatement($arr,true)->execute()->getInsertedCount();;??
  • ????????$endtime?=?microtime(true);??
  • ????????echo?'執行時間:',($endtime?-?$starttime),'秒<br?/>';??
  • ????????echo?$result;??
  • ??????
  • ????????echo?'ping';??
  • ????????//?查詢所有的記錄??
  • ????????$result?=?$mongoObj->runCommand(array('ping'=>1));??
  • ????????dump($result);??
  • ??????????
  • ????????echo?'<br/>,getLastError';??
  • ????????$error?=?$mongoObj->getLastError();??
  • ??????????
  • ????????dump($error);??
  • ??????????
  • ????????$result?=?$mongoObj->buildInfo();??
  • ??????????
  • ????????echo?'<br/>,buildInfo';??
  • ????????dump($result);??
  • ??????????
  • ????????$result?=?$mongoObj->collStats('test');??
  • ??????????
  • ????????echo?'<br/>,collStats';??
  • ????????dump($result);??
  • ??????????
  • ????????$result?=?$mongoObj->distinct('salary',array('num'=>array('$gt'=>20)));??
  • ??????????
  • ????????echo?'<br?/>,distinct';??
  • ??????????
  • ????????dump($result);??
  • ??????????
  • ????????$result?=?$mongoObj->isMaster();??
  • ??????????
  • ????????echo?'<br?/>,isMaster';??
  • ????????dump($result);??
  • ??????????
  • ????????$result?=?$mongoObj->listCommands();??
  • ??????????
  • ????????echo?'<br/>,listCommands';??
  • ??????????
  • ????????dump($result);??
  • ??????????
  • ????????$result?=?$mongoObj->listDatabases();??
  • ????????echo?'<br/>,listDatabases';??
  • ???????????
  • ????????dump($result);??
  • ???????????
  • ????????$result?=?$mongoObj->serverStatus();??
  • ????????echo?'<br/>,serverStatus';??
  • ??????????
  • ????????dump($result);??
  • ????????$result?=?$mongoObj->renameCollection('demo.hello',?'demo.hello123');??
  • ????????echo?'<br/>,renameCollection';??
  • ???????????
  • ????????dump($result);??
  • ??????????
  • ????????$result?=?$mongoObj->createCollection('ceshi');??
  • ????????echo?'<br/>,createCollection';??
  • ??????????
  • ????????dump($result);??
  • ????????$result?=?$mongoObj->dropCollection('demo1');??
  • ??????????
  • ????????echo?'<br?/>,dropCollection';??
  • ????????dump($result);??


  • 來源:http://blog.csdn.net/zhulike2011/article/details/50422391

    總結

    以上是生活随笔為你收集整理的MongoDB for PHP扩展操作类的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。