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

歡迎訪問 生活随笔!

生活随笔

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

php

PHP操作mongodb数据库操作类

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

最近的項目開發中使用的數據庫是mongodb數據庫,因為小編的公司也是剛剛使用mongodb數據庫,所以之前沒有封裝好的mongodb數據庫操作類拿來使用,所以小編在項目中自己封裝了一個mongodb數據庫操作類,特拿出來分享,不盡人意的地方希望大家勿噴。

眾所周知,mongodb是典型的nosql數據庫的代表,受到很多開發者的追捧,近幾年尤為火熱,mongodb的流行不是沒有原因的,下邊給大家簡單介紹下MongoDB。

MongoDB是一個介于關系數據庫和非關系數據庫之間的產品,是非關系數據庫當中功能最豐富,最像關系數據庫的。他支持的數據結構非常松散,是類似json的bjson格式,因此可以存儲比較復雜的數據類型。Mongo最大的特點是他支持的查詢語言非常強大,其語法有點類似于面向對象的查詢語言,幾乎可以實現類似關系數據庫單表查詢的絕大部分功能,而且還支持對數據建立索引。

它的特點是高性能、易部署、易使用,存儲數據非常方便。主要功能特性有:

面向集合存儲,易存儲對象類型的數據。

模式自由。

支持動態查詢。

支持完全索引,包含內部對象。

支持查詢。

支持復制和故障恢復。

使用高效的二進制數據存儲,包括大型對象(如視頻等)。

自動處理碎片,以支持云計算層次的擴展性

支持RUBY,PYTHON,JAVA,C++,PHP等多種語言。

文件存儲格式為BSON(一種JSON的擴展)

可通過網絡訪問
所謂“面向集合”(Collenction-Orented),意思是數據被分組存儲在數據集中,被稱為一個集合(Collenction)。每個 集合在數據庫中都有一個唯一的標識名,并且可以包含無限數目的文檔。集合的概念類似關系型數據庫(RDBMS)里的表(table),不同的是它不需要定 義任何模式(schema)。

模式自由(schema-free),意味著對于存儲在mongodb數據庫中的文件,我們不需要知道它的任何結構定義。如果需要的話,你完全可以把不同結構的文件存儲在同一個數據庫里。

存儲在集合中的文檔,被存儲為鍵-值對的形式。鍵用于唯一標識一個文檔,為字符串類型,而值則可以是各中復雜的文件類型。我們稱這種存儲形式為BSON(Binary Serialized dOcument Format)。

MongoDB服務端可運行在Linux、Windows或OS X平臺,支持32位和64位應用,默認端口為27017。推薦運行在64位平臺,因為MongoDB

在32位模式運行時支持的最大文件尺寸為2GB。

MongoDB把數據存儲在文件中(默認路徑為:/data/db),為提高效率使用內存映射文件進行管理。

小編自己封裝的PHP操作MongoDB數據庫的數據庫操作類源碼如下,僅供參考。

<?php/*** PHP操作mongodb數據庫操作類*/ class Database {protected $database = '';protected $mo;/*** 構造方法*/public function __construct() {$server = DBSERVER;$user = DBUSER;$password = DBPASS;$port = DBPORT;$database = DBNAME;$mongo = $this->getInstance($server, $user, $password, $port);$this->database = $mongo->$database;}/*** 數據庫單例方法* @param $server* @param $user* @param $password* @param $port* @return Mongo*/public function getInstance($server, $user, $password, $port) {if (isset($this->mo)) {return $this->mo;} else {if (!empty($server)) {if (!empty($port)) {if (!empty($user) && !empty($password)) {$this->mo = new Mongo("mongodb://{$user}:{$password}@{$server}:{$port}");} else {$this->mo = new Mongo("mongodb://{$server}:{$port}");}} else {$this->mo = new Mongo("mongodb://{$server}");}} else {$this->mo = new Mongo();}return $this->mo;}}/*** 查詢表中所有數據* @param $table* @param array $where* @param array $sort* @param string $limit* @param string $skip* @return array|int*/public function getAll($table, $where = array(), $sort = array(), $limit = '', $skip = '') {if (!empty($where)) {$data = $this->database->$table->find($where);} else {$data = $this->database->$table->find();}if (!empty($sort)) {$data = $data->sort($sort);}if (!empty($limit)) {$data = $data->limit($limit);}if (!empty($skip)) {$data = $data->skip($skip);}$newData = array();while ($data->hasNext()) {$newData[] = $data->getNext();}if (count($newData) == 0) {return 0;}return $newData;}/*** 查詢指定一條數據* @param $table* @param array $where* @return int*/public function getOne($table, $where = array()) {if (!empty($where)) {$data = $this->database->$table->findOne($where);} else {$data = $this->database->$table->findOne();}return $data;}/*** 統計個數* @param $table* @param array $where* @return mixed*/public function getCount($table, $where = array()) {if (!empty($where)) {$data = $this->database->$table->find($where)->count();} else {$data = $this->database->$table->find()->count();}return $data;}/*** 直接執行mongo命令* @param $sql* @return array*/public function toExcute($sql) {$result = $this->database->execute($sql);return $result;}/*** 分組統計個數* @param $table* @param $where* @param $field*/public function groupCount($table, $where, $field) {$cond = array(array('$match' => $where,),array('$group' => array('_id' => '$' . $field,'count' => array('$sum' => 1),),),array('$sort' => array("count" => -1),),);$this->database->$table->aggregate($cond);}/*** 刪除數據* @param $table* @param $where* @return array|bool*/public function toDelete($table, $where) {$re = $this->database->$table->remove($where);return $re;}/*** 插入數據* @param $table* @param $data* @return array|bool*/public function toInsert($table, $data) {$re = $this->database->$table->insert($data);return $re;}/*** 更新數據* @param $table* @param $where* @param $data* @return bool*/public function toUpdate($table, $where, $data) {$re = $this->database->$table->update($where, array('$set' => $data));return $re;}/*** 獲取唯一數據* @param $table* @param $key* @return array*/public function distinctData($table, $key, $query = array()) {if (!empty($query)) {$where = array('distinct' => $table, 'key' => $key, 'query' => $query);} else {$where = array('distinct' => $table, 'key' => $key);}$data = $this->database->command($where);return $data['values'];} } ?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207

轉自:http://www.sgphp.com/articles/55dfd799eabc8875738b45d5.html


來源:http://blog.csdn.net/u010957293/article/details/51942773

總結

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

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