Collection.php
4.5 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
191
192
193
194
195
196
197
198
199
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 15/6/13
* Time: 下午4:50
*/
namespace Hood\Dao\MongoDB;
class Collection extends Cursor
{
/**
* Enter description here...
*
* @var MongoCore
*/
private $_db;
/**
* @var string
*/
private $_dbName;
/**
* 返回的结果集
* @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) {
$result->_id = $attrs["_id"];
}
return $result;
}
/**
* 查询所有数据
* @param array $query
* @param array $fields
* @return array
*/
public function find(array $query = array(), array $fields = array())
{
return $this->_collection->find($query, $fields);
}
/**
* 查询所有符合条件的数据
* @return \MongoCursor
*/
public function findAll()
{
return $this->_cursor();
}
/**
* 查找一行数据
* @param array $query
* @param array $fields
* @return array|null
*/
public function findOne(array $query = array(), array $fields = array())
{
$_fields = empty($fields) ? $this->_fields : $fields;
return $this->_collection->findOne($query, $_fields);
}
/**
* 设置返回字段
* @param array $fields
* @return $this
*/
public function fields(array $fields)
{
$this->_fields = $fields;
return $this;
}
/**
* 更新
* @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
*/
public function findID($id, array $fields = array())
{
$_fields = empty($fields) ? $this->_fields : $fields;
if (($id instanceof \MongoId) == false) {
$id = new \MongoId($id);
}
return $this->_collection->findOne(array('_id' => $id), $_fields);
}
/**
* 保存一个文档到集合
* 如果对象来自数据库,则更新现有的数据库对象,否则插入对象
* @param $a
* @param array $options
* @return array|bool
*/
public function save($a, array $options = array())
{
return $this->_collection->save($a, $options);
}
/**
* 查询并更新数据
* @param array $query
* @param array $update
* @param array $fields
* @param array $options
* @return array
*/
public function findAndModify(array $query, array $update = NULL, array $fields = NULL, array $options = NULL)
{
return $this->_collection->findAndModify($query, $update, $fields, $options);
}
/**
* 获取总数
* @param array $query
* @return int
*/
public function count($query = array())
{
return $this->_collection->count($query);
}
/**
* 批量插入一组新的数据
* @param array $a
* @param array $options
* @return mixed
*/
public function batchInsert(array $a, array $options = array())
{
return $this->_collection->batchInsert($a, $options);
}
}