-
Notifications
You must be signed in to change notification settings - Fork 128
/
mongodb.php
541 lines (464 loc) · 11.4 KB
/
mongodb.php
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter MongoDB Active Record Library
*
* A library to interface with the NoSQL database MongoDB. For more information see http://www.mongodb.org
*
* @package CodeIgniter
* @author Alex Bilbie | www.alexbilbie.com | [email protected]
* @copyright Copyright (c) 2010, Alex Bilbie.
* @license http://codeigniter.com/user_guide/license.html
* @link http://alexbilbie.com/code/
* @version Version 0.2.2
*/
class mongo_db {
protected $CI;
private $connection;
private $db;
private $select = array();
private $where = array();
private $limit = NULL;
private $offset = NULL;
private $sort = array();
/* Constuct function
*
* Checks that the Mongo PECL library is installed and enabled
*
*/
function __construct()
{
if(!class_exists('Mongo'))
{
show_error('It looks like the MongoDB PECL extension isn\'t installed or enabled', 500);
return;
}
$this->CI =& get_instance();
// Attempt to connect
$this->connect();
}
/* Connect function
*
* Connect to a Mongo database
*
* Usage: $this->mongo_db->connect();
*/
private function connect()
{
$this->CI->config->load('mongodb');
$host = $this->CI->config->item('mongo_host');
$port = $this->CI->config->item('mongo_port');
$db = $this->CI->config->item('mongo_db');
$username = $this->CI->config->item('mongo_username');
$password = $this->CI->config->item('mongo_password');
if($host == "" || $port == "")
{
show_error('No host or port configured to connect to MongoDB', 500);
return;
}
if(!empty($db))
{
$this->db = $db;
}
else
{
show_error('No Mongo database selected', 500);
}
$auth = '';
if($username !== "" && $password !== "")
{
$auth = "{$username}:{$password}@";
}
$connection_string = "mongodb://{$auth}{$host}:{$port}/$db";
// Make the connection
try
{
$this->connection = new Mongo($connection_string);
}
catch(Exception $e)
{
show_error('Unable to connect to MongoDB. Please check your host, port, username and password settings.', 500);
}
return $this;
}
//! Get Functions
/* Select function
*
* Select specific fields from a document
*
* Usage: $this->mongo_db->select(array('foo','bar'))->get('foobar');
*/
function select($what = array())
{
if(is_array($what) && count($what) > 0)
{
$this->select = $what;
}
elseif($what !== "")
{
$this->select = array();
$this->select[] = $what;
}
return $this;
}
/* Where function
*
* Get documents where something
*
* Usage: $this->mongo_db->where(array('foo' => 1))->get('foobar');
*/
function where($where = array())
{
$this->where = $where;
return $this;
}
/* Where_in function
*
* Get documents where something is in an array of something
*
* Usage: $this->mongo_db->where_in('foo', array(1,2,3))->get('foobar');
*/
function where_in($what = "", $in = array())
{
$this->_where_init($what);
$this->where[$what]['$in'] = $in;
return $this;
}
/* Where_in function
*
* Get documents where something is in all of an array of something
*
* Usage: $this->mongo_db->where_in_all('foo', array(1,2,3))->get('foobar');
*/
function where_in_all($what = "", $in = array())
{
$this->_where_init($what);
$this->where[$what]['$all'] = $in;
return $this;
}
/* Where_not_in function
*
* Get documents where something is not in an array of something
*
* Usage: $this->mongo_db->where_not_in('foo', array(1,2,3))->get('foobar');
*/
function where_not_in($what = "", $in)
{
$this->_where_init($what);
$this->where[$what]['$nin'] = $in;
return $this;
}
/* Where_gt function
*
* Get documents where something is greater than something
*
* Usage: $this->mongo_db->where_gt('foo', 1)->get('foobar');
*/
function where_gt($what, $gt)
{
$this->_where_init($what);
$this->where[$what]['$gt'] = $gt;
return $this;
}
/* Where_gte function
*
* Get documents where something is greater than or equal to something
*
* Usage: $this->mongo_db->where_gte('foo', 1)->get('foobar');
*/
function where_gte($what, $gte)
{
$this->_where_init($what);
$this->where[$what]['$gte'] = $gte;
return $this;
}
/* Where_lt function
*
* Get documents where something is lee than something
*
* Usage: $this->mongo_db->where_lt('foo', 1)->get('foobar');
*/
function where_lt($what, $lt)
{
$this->_where_init($what);
$this->where[$what]['$lt'] = $lt;
return $this;
}
/* Where_lte function
*
* Get documents where something is less than or equal to something
*
* Usage: $this->mongo_db->where_lte('foo', 1)->get('foobar');
*/
function where_lte($what, $lte)
{
$this->_where_init($what);
$this->where[$what]['$lte'] = $lte;
return $this;
}
/* Where_lte function
*
* Get documents where something is not equal to something
*
* Usage: $this->mongo_db->where_not_equal('foo', 1)->get('foobar');
*/
function where_not_equal($what, $to)
{
$this->_where_init($what);
$this->where[$what]['$ne'] = $to;
return $this;
}
/* Order_by function
*
* Order documents by something ascending (1) or descending (-1)
*
* Usage: $this->mongo_db->order_by('foo', 1)->get('foobar');
*/
function order_by($what, $order = "ASC")
{
if($order = "ASC"){ $order = 1; }
elseif($order = "DESC"){ $order = -1; }
$this->sort[] = array($what => $order);
return $this;
}
/* Limit function
*
* Limit the returned documents by something (and optionally an offset)
*
* Usage: $this->mongo_db->limit(5,5)->get('foobar');
*/
function limit($limit = NULL, $offset = NULL)
{
if($limit !== NULL && is_numeric($limit) && $limit >= 1)
{
$this->limit = $limit;
}
if($offset !== NULL && is_numeric($offset) && $offset >= 1)
{
$this->offset = $offset;
}
return $this;
}
/* Get_where function
*
* Get documents where something
*
* Usage: $this->mongo_db->get_where('foobar', array('foo' => 'bar'));
*/
function get_where($collection = "", $where = array())
{
return $this->where($where)->get($collection);
}
/* Get function
*
* Get documents from a collection
*
* Usage: $this->mongo_db->get('foobar');
*/
function get($collection = "")
{
if($collection !== "")
{
$results = array();
$i = 0;
// Initial query
$documents = $this->connection->{$this->db}->{$collection}->find($this->where);
// Limit the results
if($this->limit !== NULL)
{
$documents = $documents->limit($this->limit);
}
// Offset the results
if($this->offset !== NULL)
{
$documents = $documents->skip($this->offset);
}
// Get the results
while($documents->hasNext())
{
$document = $documents->getNext();
if($this->select !== NULL && count($this->select) > 0)
{
foreach($this->select as $s)
{
if(isset($document[$s])){
$results[$i][$s] = $document[$s];
}
}
}
else
{
$results[$i] = $document;
}
$i++;
}
return $results;
}
else
{
show_error('No Mongo collection selected to query', 500);
}
}
/* Count function
*
* Count the number of documents
*
* Usage: $this->mongo_db->where(array('foo' => 'bar'))->count('foobar');
*/
function count($collection = "")
{
if($collection !== "")
{
// Initial query
$documents = $this->connection->{$this->db}->{$collection}->find($this->where);
// Limit the results
if($this->limit !== NULL)
{
$documents = $documents->limit($this->limit);
}
// Offset the results
if($this->offset !== NULL)
{
$documents = $documents->skip($this->offset);
}
$this->_clear();
return $documents->count();
}
else
{
$this->_clear();
show_error('No Mongo collection selected', 500);
}
}
//! Insert functions
/* Insert function
*
* Insert a new document into a collection
*
* Usage: $this->mongo_db->insert('foobar', array('foo' => 'bar'));
*/
function insert($collection = "", $insert = array())
{
if($collection == "")
{
show_error("No Mongo collection selected to insert into", 500);
}
if(count($insert) == 0 || !is_array($insert))
{
show_error("Nothing to insert into Mongo collection or insert is not an array", 500);
}
return $this->connection->{$this->db}->{$collection}->insert($insert);
}
//! Update functions
/* Update function
*
* Update a single document in a collection
*
* Usage: $this->mongo_db->where(array('foo' => 'bar'))->update('foobar', array('foo' => 'foobar'));
*/
function update($collection = "", $update = array())
{
if($collection == "")
{
show_error("No Mongo collection selected to insert into", 500);
}
if(count($update) == 0 || !is_array($update))
{
show_error("Nothing to update in Mongo collection or update is not an array", 500);
}
$update_result = $this->connection->{$this->db}->{$collection}->update($this->where, array('$set' => $update));
$this->_clear();
return $update_result;
}
/* Update function
*
* Update a all documents in a collection
*
* Usage: $this->mongo_db->where(array('foo' => 'bar'))->update('foobar', array('foo' => 'foobar'));
*/
function update_all($collection = "", $update = array())
{
if($collection == "")
{
show_error("No Mongo collection selected to insert into", 500);
}
if(count($update) == 0 || !is_array($update))
{
show_error("Nothing to update in Mongo collection or update is not an array", 500);
}
$update_result = $this->connection->{$this->db}->{$collection}->update($this->where, array('$set' => $update), array('multiple'=>TRUE));
$this->_clear();
return $update_result;
}
//! Delete functions
/* Delete function
*
* Delete a single document in a collection
*
* Usage: $this->mongo_db->delete('foobar', array('foo' => 'foobar'));
*/
function delete($collection = "", $delete = array())
{
if($collection == "")
{
show_error("No Mongo collection selected to insert into", 500);
}
if(count($delete) == 0 || !is_array($delete))
{
show_error("Nothing to delete from Mongo collection or delete is not an array", 500);
}
if(isset($delete["_id"]))
{
if(gettype($delete["_id"] == "string"))
{
$delete["_id"] = new MongoID($delete["_id"]);
}
}
return $this->connection->{$this->db}->{$collection}->remove($delete, array('justOne'=>TRUE));
}
/* Delete function
*
* Delete all documents in a collection
*
* Usage: $this->mongo_db->delete('foobar', array('foo' => 'foobar'));
*/
function delete_all($collection = "", $delete = array())
{
if($collection == "")
{
show_error("No Mongo collection selected to insert into", 500);
}
if(count($delete) == 0 || !is_array($delete))
{
show_error("Nothing to delete from Mongo collection or delete is not an array", 500);
}
if(isset($delete["_id"]))
{
if(gettype($delete["_id"] == "string"))
{
$delete["_id"] = new MongoID($delete["_id"]);
}
}
return $this->connection->{$this->db}->{$collection}->remove($delete);
}
/*
* Internal function to clear params so there are no conflicts
*/
private function _clear()
{
$this->select = array();
$this->where = array();
$this->limit = NULL;
$this->offset = NULL;
$this->sort = array();
}
/*
* Internal function to initialise parameters for where calls
*/
private function _where_init($what)
{
if(!isset($this->where[$what]))
{
$this->where[$what] = array();
}
}
}