亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 網站 > 建站經驗 > 正文

php操作MongoDB類實例

2024-04-25 20:37:29
字體:
來源:轉載
供稿:網友

本文實例講述了php操作MongoDB類的方法。分享給大家供大家參考。具體如下:

1. MyMongo.php文件:

<?php

/**

* 仿寫CI的MongoDB

* @author sparkHuang 2011-11-03

*

*/

class MyMongo {

private $mongo_config = "mongo_config.php";

private $connection;

private $db;

private $mongo_connect_string;

private $host;

private $port;

private $user;

private $pass;

private $dbname;

private $persist;

private $persist_key;

private $selects = array();

private $wheres = array();

private $sorts = array();

private $limit = 999999;

private $offset = 0;

public function __construct() {

if ( ! class_exists('Mongo')) {

$this->log_error("The MongoDB PECL extentiosn has not been installed or enabled.");

exit;

}

$this->connection_string();

$this->connect();

}

/**

* 更改數據庫

*

*/

public function switch_db($database = '') {

if (empty($database)) {

$this->log_error("To switch MongoDB databases, a new database name must be specified");

exit;

}

$this->dbname = $database;

try {

$this->db = $this->connection->{$this->dbname};

return true;

} catch(Exception $e) {

$this->log_error("Unable to switch Mongo Databases: {$e->getMessage()}");

exit;

}

}

/**

* 設置select字段

*

*/

public function select($includs = array(), $excludes = array()) {

if ( ! is_array($includs)) {

$includs = (array)$includs;

}

if ( ! is_array($excludes)) {

$excludes = (array)$excludes;

}

if ( ! empty($includs)) {

foreach ($includs as $col) {

$this->selects[$col] = 1;

}

} else {

foreach ($excludes as $col) {

$this->selects[$col] = 0;

}

}

return($this);

}

/**

* where條件查詢判斷

*

* @usage = $this->mongo_db->where(array('foo' => 'bar'))->get('foobar');

*

*/

public function where($wheres = array()) {

if ( ! is_array($wheres)) {

$wheres = (array)$wheres;

}

if ( ! empty($wheres)) {

foreach($wheres as $wh => $val) {

$this->wheres[$wh] = $val;

}

}

return($this);

}

/**

* where ... in .. 條件查詢判斷

*

* @usage = $this->mongo_db->where_in('foo', array('bar', 'zoo'))->get('foobar');

*

*/

public function where_in($field = '', $in = array()) {

$this->where_init($field);

$this->wheres[$field]['$in'] = $in;

return($this);

}

/**

* where ... not in .. 條件查詢判斷

*

* @usage = $this->mongo_db->where_not_in('foo', array('bar', 'zoo'))->get('foobar');

*

*/

public function where_not_in($field = '', $in = array()) {

$this->where_init($field);

$this->wheres[$field]['$nin'] = $in;

return($this);

}

/**

* where ... $field > $x .. 條件查詢判斷

*

* @usage = $this->mongo_db->where_gt('foo', 20)->get('foobar');

*

*/

public function where_gt($field = '', $x) {

$this->where_init($field);

$this->wheres[$field]['$gt'] = $x;

return($this);

}

/**

* where ... $field >= $x .. 條件查詢判斷

*

* @usage = $this->mongo_db->where_gte('foo', 20)->get('foobar');

*

*/

public function where_gte($field = '', $x) {

$this->where_init($field);

$this->wheres[$field]['$gte'] = $x;

return($this);

}

/**

* where ... $field < $x .. 條件查詢判斷

*

* @usage = $this->mongo_db->where_lt('foo', 20)->get('foobar');

*

*/

public function where_lt($field = '', $x) {

$this->where_init($field);

$this->wheres[$field]['$lt'] = $x;

return($this);

}

/**

* where ... $field <= $x .. 條件查詢判斷

*

* @usage = $this->mongo_db->where_lte('foo', 20)->get('foobar');

*

*/

public function where_lte($field = '', $x) {

$this->where_init($field);

$this->wheres[$field]['$lte'] = $x;

return($this);

}

/**

* where ... $field >= $x AND $field <= $y .. 條件查詢判斷

*

* @usage = $this->mongo_db->where_between('foo', 20, 30)->get('foobar');

*

*/

public function where_between($field = '', $x, $y) {

$this->where_init($field);

$this->wheres[$field]['$gte'] = $x;

$this->wheres[$field]['$lte'] = $y;

return($this);

}

/**

* where ... $field > $x AND $field < $y .. 條件查詢判斷

*

* @usage = $this->mongo_db->where_between_ne('foo', 20, 30)->get('foobar');

*

*/

public function where_between_ne($field = '', $x, $y) {

$this->where_init($field);

$this->wheres[$field]['$gt'] = $x;

$this->wheres[$field]['$lt'] = $y;

return($this);

}

/**

* where ... $field <> $x .. 條件查詢判斷

*

* @usage = $this->mongo_db->where_ne('foo', 20)->get('foobar');

*

*/

public function where_ne($field = '', $x) {

$this->where_init($field);

$this->wheres[$field]['$ne'] = $x;

return($this);

}

/**

* where ... or .. 條件查詢判斷

*

* @usage = $this->mongo_db->where_or('foo', array('foo', 'bar'))->get('foobar');

*

*/

public function where_or($field = '', $values) {

$this->where_init($field);

$this->wheres[$field]['$or'] = $values;

return($this);

}

/**

* where ... and .. 條件查詢判斷

*

* @usage = $this->mongo_db->where_and( array ( 'foo' => 1, 'b' => 'someexample' );

*/

public function where_and( $elements_values = array() ) {

foreach ( $elements_values as $element => $val ) {

$this->wheres[$element] = $val;

}

return($this);

}

/**

* where $field % $num = $result

*

* @usage = $this->mongo_db->where_mod( 'foo', 10, 1 );

*/

public function where_mod( $field, $num, $result ) {

$this->where_init($field);

$this->wheres[$field]['$mod'] = array($num, $result);

return($this);

}

/**

* where size

*

* Get the documents where the size of a field is in a given $size int

*

* @usage : $this->mongo_db->where_size('foo', 1)->get('foobar');

*/

public function where_size($field = "", $size = "") {

$this->where_init($field);

$this->wheres[$field]['$size'] = $size;

return ($this);

}

/**

* like條件查詢(PHP中定義MongoRegex類實現)

*

* @usage : $this->mongo_db->like('foo', 'bar', 'im', false, false)->get();

*/

public function like($field = "", $value = "", $flags = "i", $enable_start_wildcard = true, $enable_end_wildcard = true) {

$field = (string)$field;

$this->where_init($field);

$value = (string)$value;

$value = quotmeta($value);

if (true !== $enable_start_wildcard) {

$value = "^".$value;

}

if (true !== $enable_end_wildcard) {

$value .= "$";

}

$regex = "/$value/$flags";

$this->wheres[$field] = new MongoRegex($regex);

return($this);

}

/**

* order排序( 1 => ASC, -1 => DESC)

*

* @usage: $this->mongo_db->get_where('foo', array('name' => 'tom'))->order_by(array("age" => 1));

*/

public function order_by($fields = array()) {

foreach($fields as $col => $val) {

if ($val == -1 || $val == false || strtolower($val) == "desc") {

$this->sorts[$col] = -1;

} else {

$this->sorts[$col] = 1;

}

}

return($this);

}

/**

* limit

*

* @usage: $this->mongo_db->get_where('foo', array('name' => 'tom'))->limit(10);

*/

public function limit($x = 999999) {

if ($x !== NULL && is_numeric($x) && $x >= 1) {

$this->limit = (int)$x;

}

return($this);

}

/**

* offset

*

* @usage: $this->mongo_db->get_where('foo', array('name' => 'tom'))->offset(10);

*/

public function offset($x = 0) {

if($x !== NULL && is_numeric($x) && $x >= 1) {

$this->offset = (int) $x;

}

return($this);

}

/**

* get_where

*

* @usage: $this->mongo_db->get_where('foo', array('bar' => 'something'));

*/

public function get_where($collection = "", $where = array(), $limit = 999999) {

return($this->where($where)->limit($limit)->get($collection));

}

/**

* get

*

* @usage: $this->mongo_db->where(array('name' => 'tom'))->get('foo');

*/

public function get($collection) {

if (empty($collection)) {

$this->log_error("In order to retreive documents from MongoDB, a collection name must be passed");

exit;

}

$results = array();

$results = $this->db->{$collection}->find($this->wheres, $this->selects)->limit((int)$this->limit)->skip((int)$this->offset)->sort($this->sorts);

$returns = array();

foreach($results as $result) {

$returns[] = $result;

}

$this->clear();

return($returns);

}

/**

* count

*

* @usage: $this->db->get_where('foo', array('name' => 'tom'))->count('foo');

*/

public function count($collection) {

if (empty($collection)) {

$this->log_error("In order to retreive documents from MongoDB, a collection name must be passed");

exit;

}

$count = $this->db->{$collection}->find($this->wheres)->limit((int)$this->limit)->skip((int)$this->offset)->count();

$this->clear();

return($count);

}

/**

* insert

*

* @usage: $this->mongo_db->insert('foo', array('name' => 'tom'));

*/

public function insert($collection = "", $data = array()) {

if (empty($collection)) {

$this->log_error("No Mongo collection selected to delete from");

exit;

}

if (count($data) == 0 || ! is_array($data)) {

$this->log_error("Nothing to insert into Mongo collection or insert is not an array");

exit;

}

try {

$this->db->{$collection}->insert($data, array('fsync' => true));

if (isset($data['_id'])) {

return($data['_id']);

} else {

return(false);

}

} catch(MongoCursorException $e) {

$this->log_error("Insert of data into MongoDB failed: {$e->getMessage()}");

exit;

}

}

/**

* update : 利用MongoDB的 $set 實現

*

* @usage : $this->mongo_db->where(array('name' => 'tom'))->update('foo', array('age' => 24))

*/

public function update($collection = "", $data = array()) {

if (empty($collection)) {

$this->log_error("No Mongo collection selected to delete from");

exit;

}

if (count($data) == 0 || ! is_array($data)) {

$this->log_error("Nothing to update in Mongo collection or update is not an array");

exit;

}

try {

$this->db->{$collection}->update($this->wheres, array('$set' => $data), array('fsync' => true, 'multiple' => false)); //注意: multiple為false

return(true);

} catch(MongoCursorException $e) {

$this->log_error("Update of data into MongoDB failed: {$e->getMessage()}");

exit;

}

}

/**

* update_all : 利用MongoDB的 $set 實現

*

* @usage : $this->mongo_db->where(array('name' => 'tom'))->update_all('foo', array('age' => 24));

*/

public function update_all($collection = "", $data = array()) {

if (empty($collection)) {

$this->log_error("No Mongo collection selected to delete from");

exit;

}

if (count($data) == 0 || ! is_array($data)) {

$this->log_error("Nothing to update in Mongo collection or update is not an array");

exit;

}

try {

$this->db->{$collection}->update($this->wheres, array('$set' => $data), array('fsync' => true, 'multiple' => true)); //注意: multiple為true

return(true);

} catch(MongoCursorException $e) {

$this->log_error("Update of data into MongoDB failed: {$e->getMessage()}");

exit;

}

}

/**

* delete

*

* @usage : $this->mongo_db->where(array('name' => 'tom'))->delete('foo');

*/

public function delete($collection = "") {

if (empty($collection)) {

$this->log_error("No Mongo collection selected to delete from");

exit;

}

try {

$this->db->{$collection}->remove($this->wheres, array('fsync' => true, 'justOne' => true)); //注意justOne為true;

} catch(MongoCursorException $e) {

$this->log_error("Delete of data into MongoDB failed: {$e->getMessage()}");

exit;

}

}

/**

* delete_all

*

* @usage : $this->mongo_db->where(array('name' => 'tom'))->delete_all('foo');

*/

public function delete_all($collection = "") {

if (empty($collection)) {

$this->log_error("No Mongo collection selected to delete from");

exit;

}

try {

$this->db->{$collection}->remove($this->wheres, array('fsync' => true, 'justOne' => false)); //注意justOne為false;

} catch(MongoCursorException $e) {

$this->log_error("Delete of data into MongoDB failed: {$e->getMessage()}");

exit;

}

}

/**

* add_index

*

* @usage : $this->mongo_db->add_index('foo', array('first_name' => 'ASC', 'last_name' => -1), array('unique' => true)));

*/

public function add_index($collection, $keys = array(), $options = array()) {

if (empty($collection)) {

$this->log_error("No Mongo collection specified to add index to");

exit;

}

if (empty($keys) || ! is_array($keys)) {

$this->log_error("Index could not be created to MongoDB Collection because no keys were specified");

exit;

}

foreach($keys as $col => $val) {

if ($val == -1 || $val == false || strtolower($val) == 'desc') {

$keys[$col] = -1;

} else {

$keys[$col] = 1;

}

}

//在此沒有對$options數組的有效性進行驗證

if (true == $this->db->{$collection}->ensureIndex($keys, $options)) {

$this->clear();

return($this);

} else {

$this->log_error("An error occured when trying to add an index to MongoDB Collection");

exit;

}

}

/**

* remove_index

*

* @usage : $this->mongo_db->remove_index('foo', array('first_name' => 'ASC', 'last_name' => -1))

*/

public function remove_index($collection = "", $keys = array()) {

if (empty($collection)) {

$this->log_error("No Mongo collection specified to add index to");

exit;

}

if (empty($keys) || ! is_array($keys)) {

$this->log_error("Index could not be created to MongoDB Collection because no keys were specified");

exit;

}

if ($this->db->{$collection}->deleteIndex($keys)) {

$this->clear();

return($this);

} else {

$this->log_error("An error occured when trying to add an index to MongoDB Collection");

exit;

}

}

/**

* remove_all_index

*

* @usage : $this->mongo_db->remove_all_index('foo', array('first_name' => 'ASC', 'last_name' => -1))

*/

public function remove_all_index($collection = "", $keys = array()) {

if (empty($collection)) {

$this->log_error("No Mongo collection specified to add index to");

exit;

}

if (empty($keys) || ! is_array($keys)) {

$this->log_error("Index could not be created to MongoDB Collection because no keys were specified");

exit;

}

if ($this->db->{$collection}->deleteIndexes($keys)) {

$this->clear();

return($this);

} else {

$this->log_error("An error occured when trying to add an index to MongoDB Collection");

exit;

}

}

/**

* list_indexes

*

* @usage : $this->mongo_db->list_indexes('foo');

*/

public function list_indexes($collection = "") {

if (empty($collection)) {

$this->log_error("No Mongo collection specified to add index to");

exit;

}

return($this->db->{$collection}->getIndexInfo());

}

/**

* drop_collection

*

* @usage : $this->mongo_db->drop_collection('foo');

*/

public function drop_collection($collection = "") {

if (empty($collection)) {

$this->log_error("No Mongo collection specified to add index to");

exit;

}

$this->db->{$collection}->drop();

return(true);

}

/**

* 生成連接MongoDB 參數字符串

*

*/

private function connection_string() {

include_once($this->mongo_config);

$this->host = trim($config['host']);

$this->port = trim($config['port']);

$this->user = trim($config['user']);

$this->pass = trim($config['pass']);

$this->dbname = trim($config['dbname']);

$this->persist = trim($config['persist']);

$this->persist_key = trim($config['persist_key']);

$connection_string = "mongodb://";

if (empty($this->host)) {

$this->log_error("The Host must be set to connect to MongoDB");

exit;

}

if (empty($this->dbname)) {

$this->log_error("The Database must be set to connect to MongoDB");

exit;

}

if ( ! empty($this->user) && ! empty($this->pass)) {

$connection_string .= "{$this->user}:{$this->pass}@";

}

if ( isset($this->port) && ! empty($this->port)) {

$connection_string .= "{$this->host}:{$this->port}";

} else {

$connection_string .= "{$this->host}";

}

$this->connection_string = trim($connection_string);

}

/**

* 連接MongoDB 獲取數據庫操作句柄

*

*/

private function connect() {

$options = array();

if (true === $this->persist) {

$options['persist'] = isset($this->persist_key) && ! empty($this->persist_key) ? $this->persist_key : "ci_mongo_persist";

}

try {

$this->connection = new Mongo($this->connection_string, $options);

$this->db = $this->connection->{$this->dbname};

return ($this);

} catch (MongoConnectionException $e) {

$this->log_error("Unable to connect to MongoDB: {$e->getMessage()}");

}

}

/**

* 初始化清理部分成員變量

*

*/

private function clear() {

$this->selects = array();

$this->wheres = array();

$this->limit = NULL;

$this->offset = NULL;

$this->sorts = array();

}

/**

* 依據字段名初始化處理$wheres數組

*

*/

private function where_init($param) {

if ( ! isset($this->wheres[$param])) {

$this->wheres[$param] = array();

}

}

/**

* 錯誤記錄

*

*/

private function log_error($msg) {

$msg = "[Date: ".date("Y-m-i H:i:s")."] ".$msg;

@file_put_contents("./error.log", print_r($msg."/n", true), FILE_APPEND);

}

}

/* End of MyMongo.php */

2. mongo_config.php配置文件:

<?php

$config["host"] = "localhost";

$config["user"] = "";

$config["pass"] = "";

$config["port"] = 27017;

$config["dbname"] = "test";

$config['persist'] = TRUE;

$config['persist_key'] = 'ci_mongo_persist';

/*End of mongo_config.php*/

3. MyMongoDemo.php文件:

?<?php

include_once("MyMongo.php");

$conn = new MyMongo();

//刪除所有記錄

$conn->delete_all("blog");

//插入第一條記錄

$value = array("name" => "小明", "age" => 25, "addr" => array("country" => "中國", "province" => "廣西", "city" => "桂林"));

$conn->insert("blog", $value);

var_dump($conn->select(array("name", "age"))->get("blog"));

var_dump($conn->get("blog"));

/* End of MyMongoDemo.php */

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产精品丝袜高跟| 亚洲毛片一区二区| yellow中文字幕久久| 午夜精品久久久久久久久久久久久| 成人免费福利在线| 91av免费观看91av精品在线| 久久成人这里只有精品| 国产精品一久久香蕉国产线看观看| 久久久www成人免费精品张筱雨| 久久精品99久久久久久久久| 欧美一级片在线播放| 久久在精品线影院精品国产| 91香蕉嫩草神马影院在线观看| 一区二区三区黄色| 国产精品美女主播在线观看纯欲| 亚洲欧洲中文天堂| 欧美性猛交xxxx免费看| 亚洲精品久久久久久久久久久久| 国产精品视频不卡| 欧美激情久久久久| 日本一区二区三区四区视频| 欲色天天网综合久久| 国产精品欧美久久久| 欧美日韩激情美女| 2018日韩中文字幕| 国产精品视频久| 欧美不卡视频一区发布| 国产精品中文久久久久久久| 亚洲成人黄色在线| 欧美日本国产在线| 久久久久久九九九| 日韩中文字幕在线看| 国产精品91久久| 欧美午夜精品久久久久久久| 中日韩美女免费视频网站在线观看| 88国产精品欧美一区二区三区| 日韩美女av在线免费观看| 久久精品国产电影| 日韩在线播放视频| 九九热最新视频//这里只有精品| 国色天香2019中文字幕在线观看| 成人国产精品一区二区| 国产精品第一第二| 92国产精品视频| 亚洲精品成人网| 三级精品视频久久久久| 国产精品aaa| 精品动漫一区二区三区| 91天堂在线观看| 国产精品久久久久久久9999| 欧美国产亚洲视频| 中文字幕日韩欧美精品在线观看| 欧美激情图片区| 国产精品一区久久| 自拍偷拍免费精品| 日韩在线观看电影| 91地址最新发布| 午夜精品久久久久久久男人的天堂| 亚洲free性xxxx护士白浆| 亚洲欧美在线第一页| 91精品在线一区| 欧洲美女7788成人免费视频| 国产成人拍精品视频午夜网站| 精品国产欧美一区二区三区成人| 日本高清视频精品| 亚洲新声在线观看| 亚洲91精品在线观看| 高潮白浆女日韩av免费看| 亚洲欧美一区二区三区情侣bbw| 91亚洲国产成人久久精品网站| 日韩av高清不卡| 97在线日本国产| 国产一区二区三区欧美| 欧美精品免费在线| 日韩视频欧美视频| 国产美女高潮久久白浆| 亚洲免费视频在线观看| 成人在线小视频| 国产va免费精品高清在线观看| 亚洲男人的天堂网站| 国产精品96久久久久久| 国产在线观看精品| 精品欧美激情精品一区| 97精品欧美一区二区三区| 狠狠久久五月精品中文字幕| 国产精品一区二区三区久久久| 亚洲欧洲黄色网| 亚洲一区免费网站| 亚洲视频axxx| 国产精品视频区| 欧美主播福利视频| 日韩在线视频观看正片免费网站| 国产aaa精品| 亚洲偷熟乱区亚洲香蕉av| 久热精品视频在线免费观看| 精品动漫一区二区三区| 欧美尤物巨大精品爽| 亚洲精品wwww| 成人在线小视频| 日韩女在线观看| 亚洲美女视频网| 亚洲男子天堂网| 欧美激情videos| 中文字幕欧美日韩| 欧美精品18videos性欧美| 欧美国产高跟鞋裸体秀xxxhd| 亚洲精品美女在线观看| 国产日韩欧美日韩大片| 奇米4444一区二区三区| 日韩欧美在线中文字幕| 91久久中文字幕| 日韩精品中文在线观看| 欧美亚洲在线视频| 国产精品偷伦视频免费观看国产| 一区二区三区亚洲| 精品久久久久久久久久久久| 亚洲日本成人女熟在线观看| 中日韩美女免费视频网站在线观看| 伊人久久久久久久久久| 精品露脸国产偷人在视频| 国产精品丝袜久久久久久高清| 在线成人激情黄色| 亚洲国产精品视频在线观看| 91九色蝌蚪国产| 国产欧美日韩精品专区| 日韩日本欧美亚洲| 欧美激情免费观看| 欧美性猛交xxxx| 久久国产精品久久久久久| 欧美在线观看www| 日韩国产一区三区| 久久6免费高清热精品| 欧洲亚洲免费视频| 亚洲乱码国产乱码精品精天堂| 俺去亚洲欧洲欧美日韩| 午夜精品福利视频| 国产一区私人高清影院| 日韩最新免费不卡| 国产亚洲视频中文字幕视频| 91色在线观看| 7m第一福利500精品视频| 日韩久久午夜影院| 日韩欧美在线视频日韩欧美在线视频| 欧美裸体xxxx极品少妇| 国产一区二区日韩精品欧美精品| 日韩精品www| 92裸体在线视频网站| 国产精品十八以下禁看| 国产免费一区二区三区在线观看| 精品五月天久久| 精品国产91久久久久久老师| 亚洲最大av网| 亚洲视频在线观看免费| 色综合久久久888| 国产精品成人va在线观看| 国内精品模特av私拍在线观看| 日韩成人在线视频网站| 久久在线免费观看视频| 国产亚洲精品成人av久久ww| 国产精品久久激情| 全色精品综合影院| 国模叶桐国产精品一区| 亚洲高清av在线| 亚洲伦理中文字幕|