Collection.php
4.38 KB
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
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 15/6/13
* Time: 下午4:50
*/
namespace Hood\Dao\MongoDB;
class Collection
{
/**
* Enter description here...
*
* @var MongoCore
*/
private $_db;
/**
* @var string
*/
private $_dbName;
/**
* @var \MongoCollection
*/
private $_collection;
/**
* 条件
* @var array
*/
private $_attrs = array();
/**
* 返回的结果集
* @var array
*/
private $fields = array();
/**
* @var string
*/
private $_collectionName;
private $_set = false;
public function __construct(\MongoClient $mongo, $dbName, $collection)
{
$this->_dbName = $dbName;
$this->_collectionName = $collection;
$this->_db = $mongo->selectDB($this->_dbName);
$this->_collection = $mongo->selectCollection($this->_dbName, $this->_collectionName);
}
/**
* 插入数据
* @param $attrs
* @param array $options
* @return MongoResult
*/
public function insert($attrs, array $options = array())
{
$result = new MongoResult();
$result->status = $bool = $this->_collection->insert($attrs, $options);
$result->attrs = $attrs;
if ($bool) {
if ($attrs["_id"] instanceof \MongoId) {
$result->_id = $attrs["_id"]->__toString();
} else {
$result->_id = $attrs["_id"];
}
}
return $result;
}
/**
* 查询所有数据
* @param array $query
* @param array $fields
* @return array
*/
public function find(array $query = array(), array $fields = array())
{
$_results = $this->_collection->find($query, $fields);
$rets = array();
foreach ($_results as $value) {
if (isset($value["_id"]) && ($value["_id"] instanceof \MongoId)) {
$value["_id"] = $value["_id"]->__toString();
}
$rets[] = $value;
}
return $rets;
}
/**
* 查找一行数据
* @param array $query
* @param array $fields
* @return array|null
*/
public function findOne(array $query = array(), array $fields = array())
{
$_fields = empty($fields) ? $this->fields : $fields;
$_results = $this->_collection->findOne($query, $_fields);
if (isset($_results["_id"]) && ($_results["_id"] instanceof \MongoId)) {
$_results["_id"] = $_results["_id"]->__toString();
}
return $_results;
}
/**
* 设置返回字段
* @param array $fields
* @return $this
*/
public function fields(array $fields)
{
$this->fields = $fields;
return $this;
}
/**
* 组合查询条件
* @return array
*/
private function criteria()
{
$attrs = $this->_attrs;
foreach ($this->_attrs as $attr => $values) {
if (!empty($values)) {
if (count($values) == 1) {
$attrs[$attr] = $values[0];
} else {
$attrs[$attr]['$in'] = $values;
}
}
}
foreach ($this->_conds as $key => $value) {
$attrs[$key] = $value;
}
return $attrs;
}
/**
* 更新
* @param array $criteria
* @param array $newobj
* @param array $options
* @return bool
*/
public function update(array $criteria, array $newobj, array $options = array())
{
if ($this->_set && !isset($newobj['$set'])) {
$newobj = array('$set' => $newobj);
}
return $this->_collection->update($criteria, $newobj, $options);
}
/**
* 从集合中删除记录
* @param array $criteria
* @param array $options
* @return array|bool
*/
public function remove(array $criteria = array(), array $options = array())
{
return $this->_collection->remove($criteria, $options);
}
public function set()
{
$this->_set = true;
return $this;
}
/**
* 查找一行数据,但只返回ID数据
* @param $id
* @return array|null
*/
function findID($id, array $fields = array())
{
$_fields = empty($fields) ? $this->fields : $fields;
return $this->_collection->findOne(array('_id' => new \MongoId($id)), $_fields);
}
}