1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
5
|
|
|
* To change this template file, choose Tools | Templates |
6
|
|
|
* and open the template in the editor. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Maslosoft\Mangan\Bridge; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* MongoCollection implementation (somewhat) compatible with legacy mongo extension |
13
|
|
|
* |
14
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
15
|
|
|
*/ |
16
|
|
|
class MongoCollection |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Creates a new collection |
21
|
|
|
* |
22
|
|
|
* @param MongoDB $db - Parent database. |
23
|
|
|
* @param string $name - |
24
|
|
|
* |
25
|
|
|
* @return - Returns a new collection object. |
|
|
|
|
26
|
|
|
*/ |
27
|
|
|
public function __construct(MongoDB $db, $name) |
28
|
|
|
{ |
29
|
|
|
$this->db = $db; |
|
|
|
|
30
|
|
|
$this->name = $name; |
|
|
|
|
31
|
|
|
$this->readPreference = $db->getReadPreference(); |
|
|
|
|
32
|
|
|
$this->fqn = $db->_getFullCollectionName($name); |
|
|
|
|
33
|
|
|
$this->client = $db->_getClient(); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Counts the number of documents in this collection |
38
|
|
|
* |
39
|
|
|
* @param array $query - Associative array or object with fields to |
40
|
|
|
* match. |
41
|
|
|
* @param int $limit - Specifies an upper limit to the number returned. |
42
|
|
|
* @param int $skip - Specifies a number of results to skip before |
43
|
|
|
* starting the count. |
44
|
|
|
* |
45
|
|
|
* @return int - Returns the number of documents matching the query. |
46
|
|
|
*/ |
47
|
|
|
public function count(array $query = [], $limit = 0, $skip = 0) |
48
|
|
|
{ |
49
|
|
|
$cmd = [ |
50
|
|
|
'count' => $this->name, |
51
|
|
|
'query' => $query |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
if ($limit) |
55
|
|
|
{ |
56
|
|
|
$cmd['limit'] = $limit; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($skip) |
60
|
|
|
{ |
61
|
|
|
$cmd['skip'] = $skip; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$result = $this->db->command($cmd); |
65
|
|
|
|
66
|
|
|
if (isset($result['ok'])) |
67
|
|
|
{ |
68
|
|
|
return (int) $result['n']; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Queries this collection, returning a for the result set |
76
|
|
|
* |
77
|
|
|
* @param array $query - The fields for which to search. MongoDB's |
78
|
|
|
* query language is quite extensive. |
79
|
|
|
* @param array $fields - Fields of the results to return. |
80
|
|
|
* |
81
|
|
|
* @return MongoCursor - Returns a cursor for the search results. |
82
|
|
|
*/ |
83
|
|
|
public function find(array $query = [], array $fields = []) |
84
|
|
|
{ |
85
|
|
|
return new MongoCursor($this->client, $this->fqn, $query, $fields); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Queries this collection, returning a single element |
90
|
|
|
* |
91
|
|
|
* @param array $query - The fields for which to search. MongoDB's |
92
|
|
|
* query language is quite extensive. |
93
|
|
|
* @param array $fields - Fields of the results to return. |
94
|
|
|
* |
95
|
|
|
* @return array - Returns record matching the search or NULL. |
96
|
|
|
*/ |
97
|
|
|
public function findOne($query = [], array $fields = []) |
98
|
|
|
{ |
99
|
|
|
$cursor = $this->find($query, $fields)->limit(1); |
100
|
|
|
|
101
|
|
|
return $cursor->getNext(); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.