MongoCli.php
6.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
200
201
202
203
204
205
206
207
208
209
<?php
namespace jytools;
class MongoCli
{
private $pool;
private $database;
public function __construct($host, $port, $username, $password, $database, $num = 20)
{
$this->database = $database;
$userInfo = $username ? "{$username}:{$password}@" : "";
$dsn = "mongodb://{$userInfo}{$host}:{$port}/{$database}";
$this->pool = new MongoPool($dsn, $num);
}
/*
* 创建集合
*/
public function createCollection($table, $createIndex = [])
{
try {
$mongodb = $this->pool->get();
$database = $mongodb->selectDatabase($this->database);
$collections = $database->listCollections(['filter' => ['name' => $table]]);
$collectionExists = iterator_count($collections) > 0;
if (empty($collectionExists)) {
// 集合不存在,创建集合并添加索引
$database->createCollection($table);
$collection = $database->selectCollection($table);
$collection->createIndex($createIndex);
}
$this->pool->push($mongodb);
} catch (\Exception $e) {
output($e->getMessage());
}
}
/*
* 检查集合是否存在
* */
public function checkCollection($table): bool
{
try {
$mongodb = $this->pool->get();
$database = $mongodb->selectDatabase($this->database);
$collections = $database->listCollections(['filter' => ['name' => $table]]);
$collectionExists = iterator_count($collections) > 0;
$this->pool->push($mongodb);
if (empty($collectionExists)) {
// 集合不存在
return false;
}
return true;
} catch (\Exception $e) {
output($e->getMessage());
return false;
}
}
/*
* 写入数据
* $collection->insertOne(['name' => 'John Doe']);
* */
public function insertOne($table, $data = [])
{
try {
$mongodb = $this->pool->get();
$database = $mongodb->selectDatabase($this->database);
$collection = $database->selectCollection($table);
$result = $collection->insertOne($data);
$this->pool->push($mongodb);
return $result;
} catch (\Exception $e) {
output($e->getMessage());
return false;
}
}
/*
* 批量写入数据
* $collection->insertMany([['name' => 'John Doe'],['name' => 'John Doe']]);
* */
public function insertAll($table, $data = [])
{
try {
$mongodb = $this->pool->get();
$database = $mongodb->selectDatabase($this->database);
$collection = $database->selectCollection($table);
$result = $collection->insertMany($data);
$this->pool->push($mongodb);
return $result;
} catch (\Exception $e) {
output($e->getMessage());
return [];
}
}
/*
* 查询单条数据
* $collection->findOne(['name' => 'John Doe']);
* */
public function findOne($table, $where = [])
{
try {
$mongodb = $this->pool->get();
$database = $mongodb->selectDatabase($this->database);
$collection = $database->selectCollection($table);
$result = $collection->findOne($where);
$this->pool->push($mongodb);
return $result;
} catch (\Exception $e) {
output($e->getMessage());
return [];
}
}
/*
* 查询所有数据
* $collection->findOne(['name' => 'John Doe']);
* ['t' => ['$gt' => 1]]; // 查询大于1的数据
* ['t' => ['$lt' => 1]]; // 查询小于1的数据
* ['t' => ['$gte' => 1]]; // 查询大于等于1的数据
* ['t' => ['$lte' => 1]]; // 查询小于等于1的数据
* $options = [
* 'sort' => ['field_name' => 1], // 1 表示升序,-1 表示降序,
* 'limit' => 100,
* ];
* */
public function findAll($table, $where = [], $options = [])
{
try {
$mongodb = $this->pool->get();
$database = $mongodb->selectDatabase($this->database);
$collection = $database->selectCollection($table);
$result = $collection->find($where, $options);
$this->pool->push($mongodb);
// 遍历结果集
$data = [];
if ($result) {
$arr = $result->toArray();
foreach ($arr as $item) {
$data[] = $item->getArrayCopy();
}
return $data;
}
return [];
} catch (\Exception $e) {
output($e->getMessage());
return [];
}
}
/*
* 更新数据
* $collection->updateOne(['name' => 'John Doe'], ['$set' => ['age' => 31]]);
* */
public function updateOne($table, $where = [], $upData = [])
{
try {
$mongodb = $this->pool->get();
$database = $mongodb->selectDatabase($this->database);
$collection = $database->selectCollection($table);
$result = $collection->updateOne($where, $upData);
$this->pool->push($mongodb);
return $result;
} catch (\Exception $e) {
output($e->getMessage());
return [];
}
}
/*
* 删除数据
* $collection->deleteOne(['name' => 'John Doe']);
* */
public function deleteOne($table, $where = [])
{
try {
$mongodb = $this->pool->get();
$database = $mongodb->selectDatabase($this->database);
$collection = $database->selectCollection($table);
$result = $collection->deleteOne($where);
$this->pool->push($mongodb);
return $result;
} catch (\Exception $e) {
output($e->getMessage());
return [];
}
}
/*
* 批量删除数据
* $collection->deleteMany(['name' => 'John Doe']);
* */
public function deleteBatch($table, $where = [])
{
try {
$mongodb = $this->pool->get();
$database = $mongodb->selectDatabase($this->database);
$collection = $database->selectCollection($table);
$result = $collection->deleteMany($where);
$this->pool->push($mongodb);
return $result;
} catch (\Exception $e) {
output($e->getMessage());
return [];
}
}
}