我是靠谱客的博主 俊逸铅笔,这篇文章主要介绍PHP操作Mongodb封装类完整实例,现在分享给大家,希望可以做个参考。

本文实例讲述了PHP操作Mongodb封装类。分享给大家供大家参考,具体如下:

复制代码
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<?php /** * Mongodb 基本操作API,支持基本类似关系统型数据库的操作接口 * * @version 1.0 * [说明] * * 1:该版本API实现了 Mongodb 中最基本的插入/修改/查询/删除操作的封装 * 2:其它更高级的操作可通过 $this->getMongo() 得到原生的对象,更多API请自行查阅 Mongo PHP手册,后续版本将会对增加更多的原生API封装 * 3:该类所有API接口中的 $query 查询参数的使用请以下有关 [查询条件说明文档] * 4: 如果要存储中文字符,则一定要使用 utf8 的编码. * 5:有了本类接口基本可以按关系型数据库的概念完成Mongodb的大部分开发操作。 * * [查询条件说明文档] * * 参数:array('id'=>1) * 等同:where id=1 * * 参数:array('id'=>1,'num'=>10) * 等同:where id=1 and num=10 * * 参数:array('id'=>array($mongo->cmd('>')=>5)) * 等同:where id>5 * * 参数:array('id'=>array($mongo->cmd('!=')=>5)) * 等同:where id!=5 * * 参数:array('id'=>array($mongo->cmd('>')=>5, $mongo->cmd('<')=>10)) * 等同:where id>5 and id<10 * * 参数:array('id'=>array($mongo->cmd('in')=>array(2,5,6))) * 等同:where id in (2,5,6) * * 参数:array('id'=>array($mongo->cmd('%')=>array(2,1))) * 等同:where id % 2 = 1 * * 参数:array($mongo->cmd('or') => array( array('id'=>array($mongo->cmd('>')=>5)), array('id'=>array($mongo->cmd('<')=>10)) ) ) * 等同:where id>5 or id<10 * * 参数:array('username' => new mongoRegex("/yhp.*/")) * 等同 where username like "%yhp%" **/ class Library_Mongodb { /** * Mongodb 对象句柄 * * @var object Mongo */ private $_mongo = null; /** * 当前选择的数据库 * * @var object MongoDB */ private $_db = null; /** * 修改器命令前缀 * * @var string */ private $_cmd = '$'; /** * 调试模式 TRUE 打开 FALSE 关闭 * @var boolean */ const DEBUG = TRUE; /** * 查询条件映射关系 * * @var array */ private $_condMap = array( '<' => 'lt', // id > 1 '<=' => 'lte', // id <= 10 '>' => 'gt', // id > 5 '>=' => 'gte', // id >= 4 '!=' => 'ne', // id != 4 '%' => 'mod', // id % 4 = 0 'in' => 'in', // id in (1,2,3,4) 'notin' => 'nin',// id not in (1,2,3,4) 'or' => 'or', // id=1 or id=2 'not' => 'not', // !(id=1) ); /** * 构造函数 * * @param array $config 服务器配置,默认为: * array( * 'host'=>'localhost', // 主机名或IP地址 * 'port'=>27017, // 端口 * 'cmd'=>'$', // 修改器命令前缀 * ) */ public function __construct($config = array('host' => 'xxx', 'port' => 27017, 'username' => 'xxx', 'password' => 'xxx', 'db' => 'xxx', 'cmd' => '$')){ $server = sprintf("mongodb://%s:%s@%s:%s/%s", $config['username'], $config['password'], $config['host'], $config['port'], $config['db']); // echo "connectn"; try { $this->_mongo = new MongoClient($server, array('connect'=>true));// 立即连接 }catch (MongoConnectionException $e){ if(self::DEBUG) { echo $e->getMessage(); } return false; } $this->selectDB($config['db']); // 命令前缀 if(!isset($config['cmd'])){ $this->_cmd = ini_get('mongo.cmd'); if($this->_cmd == ''){ $this->_cmd = '$'; } } } /* ==================================== 基本操作接口API ================================= */ /** * 向集合(表)中插入新文档 * * 说明: * 1:类似mysql中的: insert into $colName set id=1,name='name1'; * * @param string $colName 集合名 * @param array $sets 数据,如: array('id'=>1,'name'=>'name1') * @param boolean $safe 是否安全操作 false:不等待服务器的响应直接返回 true:等待服务器的响应(数据非常重要时推荐) * @param boolean $fsync 操作后是否立即更新到碰盘,默认情况下由服务器决定 * * @return boolean */ public function insert($colName, $sets, $safe=false, $fsync=false){ $col = $this->_getCol($colName); try { $col->insert($sets,array('w'=>$safe,'fsync'=>$fsync)); return true; }catch (MongoCursorException $e){ return false; } } /** * 保存文档 * * 说明: * 1:如果 $sets 中有字段 "_id" 的话,则更新对应的文档;否则插入新文档 * * @param string $colName 集合名 * @param array $sets 数据,如: array('id'=>1,'name'=>'name1') * @param boolean $safe 是否安全操作 false:不等待服务器的响应直接返回 true:等待服务器的响应(数据非常重要时推荐) * @param boolean $fsync 操作后是否立即更新到碰盘,默认情况下由服务器决定 * * @return boolean */ public function save($colName, $sets, $safe=false, $fsync=false){ // 处理 '_id' 字段 $sets = $this->_parseId($sets); $ret = $this->_getCol($colName)->save($sets,array('w'=>$safe,'fsync'=>$fsync)); return $ret; } /** * 删除集合中的文档记录 * * 说明: * 1:类似mysql中的: delete from $colName where id=1; * * @param string $colName 集合名 * @param array $query 查询条件,如果为空数组的话,则会删除所有记录.具体请看 [查询条件说明文档] * @param boolean $delAll 是否删除所以条例查询的记录,默认为 true,当为 false是,类似效果 delete from tab where id=1 limit 1; * @param boolean $safe 是否安全操作 false:不等待服务器的响应直接返回 true:等待服务器的响应(数据非常重要时推荐) * @param boolean $fsync 操作后是否立即更新到碰盘,默认情况下由服务器决定 * * @return boolean */ public function delete($colName,$query=array(),$delAll=true,$safe=false,$fsync=false){ // 自动处理 '_id' 字段 $query = $this->_parseId($query); // 删除选项 $options = array( 'justOne' => !$delAll, 'w' => $safe, 'fsync' => $fsync, ); $col = $this->_getCol($colName); return $col->remove($query,$options); } /** * 删除整个集合 * * 说明: * 1:集合中的索引也会被删除 * * @param string $colName 集合名 * * @return array */ public function dropCol($colName){ return $this->_getCol($colName)->drop(); } /** * 更新集合文档记录 * * 说明: * 1:类似mysql中的: update $colName set name='mongo' where id=10; * * @param string $colName 集合名 * @param array $newDoc 要更新的文档记录 * @param array $query 查询条件,如果为空数组则更新所有记录.具体请看 [查询条件说明文档] * @param string $option 操作选项,可选择项如下; * * 'set':只修改指定的字段(默认值,如果这个键不存在,则创建它。存在则更新). * 示例: update('user', array('name'=>'mongo'), array('id'=>10)); * 类似: update user set name='mongo' where id=10; * * 'inc':将指定的字段累加/减(如果值为负数则是相减,不存在键则创建。字段类型一定要是数字) * 示例:update('user', array('num'=>1), array('id'=>10), 'inc'); * 类似: update user set num=num+1 where id=10; * * 'push':将文档添加到指定键中(数组),如果键不存在则会自动创建,存在则添加到该键的尾端。 * 示例:update('user', array('comm'=>array('commid'=>1,'title'=>'title1')), array('id'=>1), 'push'); * 解说:为 id=1 的记录添加一个 comm 的评论字段,该字段对应一个 array('commid'=>1,'title'=>'title1') 的新文档。 * * 'pop':将指定键中的文档删除(数组) * 示例:update('user', array('comm'=>array('commid'=>1)), array('id'=>1), 'pop'); * 解说:删除 id=1 的记录中 comm 对应的文档集合中 'commid'=>1 对应的文档. * * 'unset':在文档中删除指定的键 * 示例:update('user', array('name'=>1), array('id'=>1), 'unset'); * 解说: 将 user 集合中将 id=1 对应的文档中的 name 字段删除 * * 'pull':删除文档中匹配其值的键 * 示例:update('user', array('name'=>'youname'), array('id'=>1), 'pull'); * 解说:将 user 集合中将 id=1 对应的文档中的 name='youname' 的字段删除 * * 'addToSet':如果值不存在就添加(避免重复添加) * 示例:update('user', array('names'=>'youname'), array('id'=>1), 'addToSet'); * 解说:向 user 集合中 id=1 对应的文档中的 names 字段添加 'youname' 这个值(不存在时才添加) * * 'replace':用 $newDoc 新文档替换 $query 所找到的文档 * 示例:update('user', array('newid'=>1,'newnames'=>'name1'), array('id'=>1), 'replace'); * 解说:将 user 集合中 id=1 对应的文档用 array('newid'=>1,'newnames'=>'name1') 的新文档替换 * * @param boolean $upAll 是否更新找到的所有记录 * @param boolean $upsert 如果查询条件不存在时,是否以查询条件和要更新的字段一起新建一个集合 * @param boolean $safe 是否安全删除 false:不等待服务器的响应直接返回 true:等待服务器的响应(数据非常重要时推荐) * @param boolean $fsync 操作后是否立即更新到碰盘,默认情况下由服务器决定 * * @return boolean */ public function update($colName,$newDoc,$query=array(),$option='set',$upAll=true,$upsert=false,$safe=false,$fsync=false){ // 自动处理 '_id' 字段 $query = $this->_parseId($query); // 得到集合 $col = $this->_getCol($colName); // 重新组合新文档 if($option != 'replace'){ $newDoc = array($this->cmd($option) => $newDoc); } // 更新条件 $options = array( 'upsert' => $upsert, 'multiple' => $upAll, 'w' => $safe, 'fsync' => $fsync, ); return $col->update($query,$newDoc,$options); } /** * 查询文档集,返回二维数组 * * 说明: * 1:类似mysql中的 select * from table * * 示例:select('user'); * 类似:select * from user; * * 示例:select('user',array('id','name')); * 类似:select id,name from user; * * 示例:select('user',array('id','name'),array('id'=>1)); * 类似:select id,name from user where id=1; * * 示例:select('user',array('id','name'),array('id'=>1),array('num'=>1)); * 类似:select id,name from user where id=1 order by num asc; * * 示例:select('user',array('id','name'),array('id'=>1),array('num'=>1),10); * 类似:select id,name from user where id=1 order by num asc limit 10; * * 示例:select('user',array('id','name'),array('id'=>1),array('num'=>1),10,2); * 类似:select id,name from user where id=1 order by num asc limit 2,10; * * * * @param string $colName 集合名 * @param array $query 查询条件,具体请看 [查询条件说明文档] * @param array $fields 结果集返回的字段, array():表示返回所有字段 array('id','name'):表示只返回字段 "id,name" * @param array $sort 排序字段, array('id'=>1):表示按id字段升序 array('id'=>-1):表示按id字段降序 array('id'=>1, 'age'=>-1):表示按id升序后再按age降序 * @param int $limit 取多少条记录 * @param int $skip 跳过多少条(从多少条开始) * * @return array */ public function select($colName,$query=array(),$fields=array(),$sort=array(),$limit=0,$skip=0){ // 得到集合 $col = $this->_getCol($colName); // 自动处理 '_id' 字段 $query = $this->_parseId($query); // 结果集偏历 $cursor = $col->find($query,$fields); // 排序 if($sort){ $cursor->sort($sort); } // 跳过记录数 if($skip > 0){ $cursor->skip($skip); } // 取多少行记录 if($limit > 0){ $cursor->limit($limit); } $result = array(); foreach($cursor as $row){ $result[] = $this->_parseArr($row); } return $result; } /** * 统计文档记录数 * * @param string $colName 集合名 * @param array $query 查询条件,具体请看 [查询条件说明文档] * @param int $limit 取多少条记录 * @param int $skip 跳过多少条 * @return unknown */ public function count($colName,$query=array(),$limit=0,$skip=0){ return $this->_getCol($colName)->count($query,$limit,$skip); } /** * 返回集合中的一条记录(一维数组) * * @param string $colName 集合名 * @param array $query 查询条件,具体请看 [查询条件说明文档] * @param array $fields 结果集返回的字段, array():表示返回所有字段 array('id','name'):表示只返回字段 "id,name" * * @return array */ public function fetchRow($colName,$query=array(), $fields=array()){ // 得到集合名 $col = $this->_getCol($colName); // 自动处理 '_id' 字段 $query = $this->_parseId($query); // 处理结果集 return $this->_parseArr($col->findOne($query,$fields)); } /** * 返回符合条件的文档中字段的值 * * @param string $colName 集合名 * @param array $query 查询条件,具体请看 [查询条件说明文档] * @param string $fields 要取其值的字段,默认为 "_id" 字段,类似mysql中的自增主键 * * @return mixed */ public function fetchOne($colName,$query=array(), $fields='_id'){ $ret = $this->fetchRow($colName,$query,array($fields)); return isset($ret[$fields]) ? $ret[$fields] : false; } /** * 返回查询文档集合集中指定字段的值(一维数组) * * @param string $colName 集合名 * @param array $query 查询条件,具体请看 [查询条件说明文档] * @param string $fields 要取其值的字段,默认为 "_id" 字段,类似mysql中的自增主键 * * @return array */ public function fetchCol($colName,$query=array(), $fields='_id'){ $result = array(); $list = $this->select($colName,$query,array($fields)); foreach ($list as $row){ $result[] = $row[$fields]; } return $result; } /** * 返回指定下标的查询文档集合(二维数组) * * @param string $colName 集合名 * @param array $query 查询条件,具体请看 [查询条件说明文档] * @param string $fields 要取其值的字段,默认为 "_id" 字段,类似mysql中的自增主键 * * @return array */ public function fetchAssoc($colName,$query=array(), $fields='_id'){ $result = array(); $list = $this->select($colName,$query); foreach ($list as $row){ $key = $row[$fields]; $result[][$key] = $row; } return $result; } /* ==================================== 辅助操作接口API ================================= */ /** * 返回命令或命令前缀 * * @param string $option 命令,如果为空时则返回命令前缀 * * @return string */ public function cmd($option=''){ // 只返回命令前缀 if($option == ''){ return $this->_cmd; } // 如果是操作符 if(isset($this->_condMap[$option])){ $option = $this->_condMap[$option]; } return $this->_cmd.$option; } /** * 选择或创建数据库(注意:新创建的数据库如果在关闭连接前没有写入数据将会被自动删除) * * @param string $dbname 数据库名 */ public function selectDB($dbname){ $this->_db = $this->_mongo->selectDB($dbname); } /** * 得到所有的数据库 * * @param boolean $onlyName 是否只返回数据库名的数组 * @return array */ public function allDB($onlyName=false){ $ary = $this->_mongo->listDBs(); if($onlyName){ $ret = array(); foreach ($ary['databases'] as $row){ $ret[] = $row['name']; } return $ret; }else{ return $ary; } } /** * 删除数据库 * * @return array */ public function dropDB($dbname){ return $this->_mongo->dropDB($dbname); } /** * 关闭连接 * */ public function close(){ $this->_mongo->close(); } /** * 得到 Mongo 原生对象,进行其它更高级的操作,详细请看PHP手册 * */ public function getMongo(){ return $this->_mongo; } /** * 返回最后的错误信息 * * @return array */ public function getError(){ return $this->_db->lastError(); } /* ======================= 以下为私有方法 ====================== */ // 解析数据组中的'_id'字段(如果有的话) private function _parseId($arr){ if(isset($arr['_id'])){ $arr['_id'] = new MongoId($arr['_id']); } return $arr; } // 得到集合对象 private function _getCol($colName){ return $this->_db->selectCollection($colName); } // 解析数组中的"_id"并且返回 private function _parseArr($arr){ if(!empty($arr)) { $ret = (array)$arr['_id']; $arr['_id'] = $ret['$id']; } return $arr; } }//End Class ?>

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP+MongoDB数据库操作技巧大全》、《PHP基于pdo操作数据库技巧总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

最后

以上就是俊逸铅笔最近收集整理的关于PHP操作Mongodb封装类完整实例的全部内容,更多相关PHP操作Mongodb封装类完整实例内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(135)

评论列表共有 0 条评论

立即
投稿
返回
顶部