|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pouzor\MongoDBBundle\Repository; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use MongoDB\BulkWriteResult; |
|
7
|
|
|
use MongoDB\Collection; |
|
8
|
|
|
use MongoDB\Operation\InsertOne; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use Psr\Log\NullLogger; |
|
11
|
|
|
use Pouzor\MongoDBBundle\Constants\DriverClasses; |
|
12
|
|
|
use Pouzor\MongoDBBundle\Constants\Query; |
|
13
|
|
|
use Pouzor\MongoDBBundle\Exception\MalformedOperationException; |
|
14
|
|
|
use Pouzor\MongoDBBundle\Services\ArrayAccessor; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
class Repository |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $indexes = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $id_field = '_id'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $name; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Collection |
|
36
|
|
|
*/ |
|
37
|
|
|
private $collection; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var NullLogger |
|
41
|
|
|
*/ |
|
42
|
|
|
private $logger; |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
private $persistence = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Repository constructor. |
|
49
|
|
|
* @param $name |
|
50
|
|
|
* @param $manager |
|
51
|
|
|
*/ |
|
52
|
15 |
|
public function __construct($name, $manager) |
|
53
|
|
|
{ |
|
54
|
|
|
|
|
55
|
15 |
|
$this->collection = $manager->getDatabase()->selectCollection($name); |
|
56
|
|
|
|
|
57
|
15 |
|
$this->name = $name; |
|
58
|
|
|
|
|
59
|
15 |
|
$this->logger = $manager->getLogger() ?: new NullLogger(); |
|
60
|
|
|
|
|
61
|
15 |
|
$this->persistence = []; |
|
62
|
|
|
|
|
63
|
15 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param $fields |
|
67
|
|
|
* @param array $options |
|
68
|
|
|
* @param null $callback |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function ensureIndex($fields, array $options = ['maxTimeMS' => 0], $rebuild = false, $callback = null) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
1 |
|
$this->logger->info('Creating index with fields ', $fields); |
|
73
|
|
|
|
|
74
|
|
|
try { |
|
75
|
1 |
|
$name = $this->collection->createIndex($fields, $options); |
|
76
|
|
|
|
|
77
|
1 |
|
if (is_callable($callback)) { |
|
78
|
1 |
|
call_user_func($callback, $name); |
|
79
|
1 |
|
} |
|
80
|
1 |
|
} catch (\Exception $re) { |
|
81
|
|
|
$this->logger->warning($re->getMessage()); |
|
82
|
|
|
|
|
83
|
|
|
$regex = '/Index with name: (?<name>\w+) already exists with different options/'; |
|
84
|
|
|
|
|
85
|
|
|
if (preg_match($regex, $re->getMessage(), $output)) { |
|
86
|
|
|
$name = $output['name']; |
|
87
|
|
|
|
|
88
|
|
|
$this->logger->info('Dropping index ' . $name); |
|
89
|
|
|
$result = $this->collection->dropIndex($name); |
|
90
|
|
|
|
|
91
|
|
|
$this->logger->info('Result from dropping index ' . $name, $result); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
$this->ensureIndex($fields, $options, $callback); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
public function dropIndexes() |
|
101
|
|
|
{ |
|
102
|
|
|
try { |
|
103
|
1 |
|
$this->collection->dropIndexes(); |
|
104
|
1 |
|
} catch (\MongoDB\Driver\Exception\RuntimeException $e) { |
|
|
|
|
|
|
105
|
|
|
//Collection doesn't exist yet |
|
106
|
|
|
} |
|
107
|
1 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param null $callback |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function buildIndexes($rebuild = false, $callback = null) |
|
113
|
|
|
{ |
|
114
|
|
|
|
|
115
|
1 |
|
if ($rebuild) { |
|
116
|
|
|
try { |
|
117
|
|
|
$this->collection->dropIndexes(); |
|
118
|
|
|
} catch (\MongoDB\Driver\Exception\RuntimeException $e) { |
|
|
|
|
|
|
119
|
|
|
//Collection doesn't exist yet |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
foreach ($this->indexes as $name => $conf) { |
|
124
|
1 |
|
if (is_array($conf)) { |
|
125
|
|
|
$this->ensureIndex( |
|
126
|
|
|
$conf['fields'], |
|
127
|
|
|
isset($conf['options']) ? $conf['options'] : [], |
|
128
|
|
|
$rebuild, |
|
129
|
|
|
$callback |
|
130
|
|
|
); |
|
131
|
|
|
} else { |
|
132
|
1 |
|
$this->ensureIndex([$name => $conf], [], $rebuild, $callback); |
|
133
|
|
|
} |
|
134
|
1 |
|
} |
|
135
|
1 |
|
} |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return int|void |
|
140
|
|
|
*/ |
|
141
|
|
|
public function countPendingOperations() |
|
142
|
|
|
{ |
|
143
|
|
|
return count($this->persistence); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Persist a document |
|
148
|
|
|
* |
|
149
|
|
|
* @param $document |
|
150
|
|
|
* @param bool $andFlush |
|
151
|
|
|
* @return BulkWriteResult |
|
152
|
|
|
*/ |
|
153
|
|
|
public function persist($document, $andFlush = false) |
|
154
|
|
|
{ |
|
155
|
|
|
|
|
156
|
|
|
$this->persistence += [$document]; |
|
157
|
|
|
|
|
158
|
|
|
if ($andFlush) { |
|
159
|
|
|
return $this->flush(); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return BulkWriteResult |
|
165
|
|
|
*/ |
|
166
|
|
|
public function flush() |
|
167
|
|
|
{ |
|
168
|
|
|
$result = $this->bulk($this->persistence); |
|
169
|
|
|
|
|
170
|
|
|
$this->persistence = []; |
|
171
|
|
|
|
|
172
|
|
|
return $result; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Write many operations in bulk |
|
178
|
|
|
* Ex: |
|
179
|
|
|
* $result = $repository->bulk([ |
|
180
|
|
|
* [ |
|
181
|
|
|
* "insertOne" => [ |
|
182
|
|
|
* [ |
|
183
|
|
|
* "name" => "Hannes Magnusson", |
|
184
|
|
|
* "company" => "10gen", |
|
185
|
|
|
* ] |
|
186
|
|
|
* ], |
|
187
|
|
|
* ], |
|
188
|
|
|
* [ |
|
189
|
|
|
* "insertOne" => [ |
|
190
|
|
|
* [ |
|
191
|
|
|
* "name" => "Jeremy Mikola", |
|
192
|
|
|
* "company" => "10gen", |
|
193
|
|
|
* ] |
|
194
|
|
|
* ], |
|
195
|
|
|
* ], |
|
196
|
|
|
* [ |
|
197
|
|
|
* "updateMany" => [ |
|
198
|
|
|
* ["company" => "10gen"], |
|
199
|
|
|
* ['$set' => ["company" => "MongoDB"]], |
|
200
|
|
|
* ], |
|
201
|
|
|
* ], |
|
202
|
|
|
* [ |
|
203
|
|
|
* "updateOne" => [ |
|
204
|
|
|
* ["name" => "Hannes Magnusson"], |
|
205
|
|
|
* ['$set' => ["viking" => true]], |
|
206
|
|
|
* ], |
|
207
|
|
|
* ], |
|
208
|
|
|
* ]); |
|
209
|
|
|
* @param $operations |
|
210
|
|
|
* @return \MongoDB\BulkWriteResult |
|
211
|
|
|
* @throws MalformedOperationException |
|
212
|
|
|
*/ |
|
213
|
|
|
public function bulk($operations) |
|
214
|
|
|
{ |
|
215
|
|
|
|
|
216
|
|
|
$this->validateBulkOperations($operations); |
|
217
|
|
|
|
|
218
|
|
|
$this->logger->info( |
|
219
|
|
|
'Bulk operations ', |
|
220
|
|
|
[ |
|
221
|
|
|
'operations' => $operations, |
|
222
|
|
|
'col' => $this->name |
|
223
|
|
|
] |
|
224
|
|
|
); |
|
225
|
|
|
|
|
226
|
|
|
$result = $this->collection->bulkWrite($operations); |
|
227
|
|
|
|
|
228
|
|
|
$this->logger->info( |
|
229
|
|
|
'Bulk operations result', |
|
230
|
|
|
[ |
|
231
|
|
|
'inserted' => $result->getInsertedCount(), |
|
232
|
|
|
'upserted' => $result->getUpsertedCount(), |
|
233
|
|
|
'updated' => $result->getModifiedCount(), |
|
234
|
|
|
'deleted' => $result->getDeletedCount(), |
|
235
|
|
|
'col' => $this->name |
|
236
|
|
|
] |
|
237
|
|
|
); |
|
238
|
|
|
|
|
239
|
|
|
return $result; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @param array $query |
|
244
|
|
|
* @param array $options |
|
245
|
|
|
* @return \MongoDB\Driver\Cursor |
|
246
|
|
|
*/ |
|
247
|
2 |
|
public function findBy(array $query = [], array $options = ['maxTimeMS' => 0]) |
|
248
|
|
|
{ |
|
249
|
2 |
|
$this->validateQueryOptions($options); |
|
250
|
|
|
|
|
251
|
2 |
|
$this->logger->info( |
|
252
|
2 |
|
'Find many by', |
|
253
|
|
|
[ |
|
254
|
2 |
|
'query' => $query, |
|
255
|
2 |
|
'col' => $this->name, |
|
256
|
|
|
] |
|
257
|
2 |
|
); |
|
258
|
|
|
|
|
259
|
2 |
|
return $this->collection->find($query, $options); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Find one document |
|
265
|
|
|
* |
|
266
|
|
|
* @param array $filter |
|
267
|
|
|
* @param array $options |
|
268
|
|
|
* @return \MongoDB\Driver\Cursor |
|
269
|
|
|
*/ |
|
270
|
1 |
View Code Duplication |
public function findOneBy(array $filter = [], array $options = ['maxTimeMS' => 0]) |
|
|
|
|
|
|
271
|
|
|
{ |
|
272
|
1 |
|
$this->validateQueryOptions($options); |
|
273
|
|
|
|
|
274
|
1 |
|
$this->logger->info( |
|
275
|
1 |
|
'Find one ', |
|
276
|
|
|
[ |
|
277
|
1 |
|
'query' => $filter, |
|
278
|
1 |
|
'col' => $this->name, |
|
279
|
|
|
] |
|
280
|
1 |
|
); |
|
281
|
|
|
|
|
282
|
1 |
|
return $this->collection->findOne($filter, $options); |
|
|
|
|
|
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @param $id |
|
288
|
|
|
* @param array $options |
|
289
|
|
|
* @return null|object |
|
290
|
|
|
*/ |
|
291
|
2 |
View Code Duplication |
public function find($id, array $options = ['maxTimeMS' => 0]) |
|
|
|
|
|
|
292
|
|
|
{ |
|
293
|
2 |
|
$this->validateQueryOptions($options); |
|
294
|
|
|
|
|
295
|
2 |
|
$this->logger->info( |
|
296
|
2 |
|
'Mongo find by id', |
|
297
|
|
|
[ |
|
298
|
2 |
|
'id' => $id, |
|
299
|
2 |
|
'col' => $this->name |
|
300
|
2 |
|
] |
|
301
|
2 |
|
); |
|
302
|
|
|
|
|
303
|
2 |
|
return $this->collection->findOne([$this->getIdField() => self::createObjectId($id)], $options); |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* @param array $documents |
|
308
|
|
|
* @return \MongoDB\InsertManyResult |
|
309
|
|
|
*/ |
|
310
|
8 |
|
public function insertMany(array $documents = []) |
|
311
|
|
|
{ |
|
312
|
8 |
|
$result = $this->collection->insertMany($documents); |
|
313
|
|
|
|
|
314
|
8 |
|
$this->logger->info( |
|
315
|
8 |
|
'Insert many result', |
|
316
|
|
|
[ |
|
317
|
8 |
|
'result' => $result, |
|
318
|
8 |
|
'col' => $this->name, |
|
319
|
1 |
|
] |
|
320
|
8 |
|
); |
|
321
|
|
|
|
|
322
|
8 |
|
return $result; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
|
|
/** |
|
326
|
|
|
* @param $document |
|
327
|
|
|
* @return \MongoDB\InsertOneResult |
|
328
|
|
|
*/ |
|
329
|
4 |
|
public function insertOne($document) |
|
330
|
|
|
{ |
|
331
|
4 |
|
$this->logger->info( |
|
332
|
4 |
|
'Insert one', |
|
333
|
|
|
[ |
|
334
|
4 |
|
'document' => $document, |
|
335
|
4 |
|
'col' => $this->name, |
|
336
|
|
|
] |
|
337
|
4 |
|
); |
|
338
|
|
|
|
|
339
|
4 |
|
return $this->collection->insertOne($document); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
2 |
|
public function update($id, array $modifications = [], array $options = []) |
|
343
|
|
|
{ |
|
344
|
|
|
|
|
345
|
1 |
|
$this->updateOne( |
|
346
|
1 |
|
[$this->getIdField() => self::createObjectId($id)], |
|
347
|
2 |
|
$modifications, |
|
348
|
|
|
$options |
|
349
|
1 |
|
); |
|
350
|
1 |
|
} |
|
351
|
|
|
|
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* Update one document |
|
355
|
|
|
* |
|
356
|
|
|
* @param array $where |
|
357
|
|
|
* @param array $modifications |
|
358
|
|
|
* @param array $options |
|
359
|
|
|
* @return \MongoDB\UpdateResult |
|
360
|
|
|
*/ |
|
361
|
2 |
View Code Duplication |
public function updateOne(array $where = [], array $modifications = [], array $options = []) |
|
362
|
|
|
{ |
|
363
|
|
|
|
|
364
|
1 |
|
$this->logger->info( |
|
365
|
1 |
|
'Update one ', |
|
366
|
|
|
[ |
|
367
|
1 |
|
'where' => $where, |
|
368
|
1 |
|
'update' => $modifications, |
|
369
|
1 |
|
'col' => $this->name, |
|
370
|
|
|
] |
|
371
|
2 |
|
); |
|
372
|
|
|
|
|
373
|
1 |
|
return $this->collection->updateOne($where, $modifications, $options); |
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
|
|
377
|
|
|
/** |
|
378
|
|
|
* Update many documents at the same time |
|
379
|
|
|
* |
|
380
|
|
|
* @param array $where |
|
381
|
|
|
* @param array $modifications |
|
382
|
|
|
* @param array $options |
|
383
|
|
|
* @return \MongoDB\UpdateResult |
|
384
|
|
|
*/ |
|
385
|
1 |
View Code Duplication |
public function updateMany(array $where = [], array $modifications = [], array $options = []) |
|
386
|
|
|
{ |
|
387
|
|
|
|
|
388
|
1 |
|
$this->logger->info( |
|
389
|
1 |
|
'Update many ', |
|
390
|
|
|
[ |
|
391
|
1 |
|
'where' => $where, |
|
392
|
1 |
|
'update' => $modifications, |
|
393
|
1 |
|
'col' => $this->name, |
|
394
|
|
|
] |
|
395
|
1 |
|
); |
|
396
|
|
|
|
|
397
|
1 |
|
return $this->collection->updateMany($where, $modifications, $options); |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
/** |
|
401
|
|
|
* Update one documents (the whole document) |
|
402
|
|
|
* |
|
403
|
|
|
* @param array $where |
|
404
|
|
|
* @param array $modifications |
|
405
|
|
|
* @param array $options |
|
406
|
|
|
* @return \MongoDB\UpdateResult |
|
407
|
|
|
*/ |
|
408
|
1 |
View Code Duplication |
public function replaceOne(array $where = [], array $modifications = [], array $options = []) |
|
409
|
|
|
{ |
|
410
|
|
|
|
|
411
|
1 |
|
$this->logger->info( |
|
412
|
1 |
|
'Replace one ', |
|
413
|
|
|
[ |
|
414
|
1 |
|
'where' => $where, |
|
415
|
1 |
|
'update' => $modifications, |
|
416
|
1 |
|
'col' => $this->name, |
|
417
|
|
|
] |
|
418
|
1 |
|
); |
|
419
|
|
|
|
|
420
|
1 |
|
return $this->collection->replaceOne($where, $modifications, $options); |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
|
|
424
|
|
|
/** |
|
425
|
|
|
* @param $id |
|
426
|
|
|
* @param array $options |
|
427
|
|
|
* @return \MongoDB\DeleteResult |
|
428
|
|
|
*/ |
|
429
|
1 |
|
public function delete($id, array $options = []) |
|
430
|
|
|
{ |
|
431
|
1 |
|
return $this->deleteOne( |
|
432
|
1 |
|
[$this->getIdField() => self::createObjectId($id)], |
|
433
|
|
|
$options |
|
434
|
1 |
|
); |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
/** |
|
438
|
|
|
* Delete one document |
|
439
|
|
|
* |
|
440
|
|
|
* @param array $where |
|
441
|
|
|
* @param array $options |
|
442
|
|
|
* @return \MongoDB\DeleteResult |
|
443
|
|
|
*/ |
|
444
|
2 |
|
public function deleteOne(array $where = [], array $options = []) |
|
445
|
|
|
{ |
|
446
|
|
|
|
|
447
|
2 |
|
$this->logger->info( |
|
448
|
2 |
|
'Delete one ', |
|
449
|
|
|
[ |
|
450
|
2 |
|
'where' => $where, |
|
451
|
2 |
|
'col' => $this->name, |
|
452
|
|
|
] |
|
453
|
2 |
|
); |
|
454
|
|
|
|
|
455
|
2 |
|
return $this->collection->deleteOne($where, $options); |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
/** |
|
459
|
|
|
* @param array $where |
|
460
|
|
|
* @param array $options |
|
461
|
|
|
* @return \MongoDB\DeleteResult |
|
462
|
|
|
*/ |
|
463
|
15 |
|
public function deleteMany(array $where = [], array $options = []) |
|
464
|
|
|
{ |
|
465
|
|
|
|
|
466
|
15 |
|
$this->logger->info( |
|
467
|
15 |
|
'Delete many ', |
|
468
|
|
|
[ |
|
469
|
15 |
|
'where' => $where, |
|
470
|
15 |
|
'col' => $this->name, |
|
471
|
|
|
] |
|
472
|
15 |
|
); |
|
473
|
|
|
|
|
474
|
15 |
|
return $this->collection->deleteMany($where, $options); |
|
475
|
|
|
} |
|
476
|
|
|
|
|
477
|
|
|
/** |
|
478
|
|
|
* Aggregation function for mongoDB |
|
479
|
|
|
* @param array $pipelines |
|
480
|
|
|
* @param array $options |
|
481
|
|
|
* @return \Traversable |
|
482
|
|
|
*/ |
|
483
|
2 |
|
public function aggregate(array $pipelines = [], array $options = []) |
|
484
|
|
|
{ |
|
485
|
|
|
|
|
486
|
1 |
|
$this->logger->info( |
|
487
|
1 |
|
'Aggregate ', |
|
488
|
|
|
[ |
|
489
|
1 |
|
'pipelines' => $pipelines, |
|
490
|
2 |
|
'options' => $options, |
|
491
|
1 |
|
'col' => $this->name, |
|
492
|
|
|
|
|
493
|
|
|
] |
|
494
|
1 |
|
); |
|
495
|
|
|
|
|
496
|
1 |
|
return $this->collection->aggregate($pipelines, $options); |
|
497
|
|
|
} |
|
498
|
|
|
|
|
499
|
|
|
/** |
|
500
|
|
|
* @param $field |
|
501
|
|
|
* @param null $min |
|
502
|
|
|
* @param null $max |
|
503
|
|
|
* @param array $options |
|
504
|
|
|
* @return \MongoDB\Driver\Cursor |
|
505
|
|
|
*/ |
|
506
|
|
|
public function findBetween($field, $min = null, $max = null, array $options = []) |
|
507
|
|
|
{ |
|
508
|
|
|
$query = [ |
|
509
|
|
|
$field => ['$gt' => $min, '$lt' => $max] |
|
510
|
|
|
]; |
|
511
|
|
|
|
|
512
|
|
|
return $this->findBy($query, $options); |
|
513
|
|
|
} |
|
514
|
|
|
|
|
515
|
|
|
/** |
|
516
|
|
|
* @param $field |
|
517
|
|
|
* @param $min |
|
518
|
|
|
* @param $max |
|
519
|
|
|
* @param array $options |
|
520
|
|
|
* @return int |
|
521
|
|
|
*/ |
|
522
|
|
|
public function countBetween($field, $min = null, $max = null, array $options = ['maxTimeMS' => 0]) |
|
523
|
|
|
{ |
|
524
|
|
|
$query = [$field => ['$gt' => $min, '$lte' => $max]]; |
|
525
|
|
|
|
|
526
|
|
|
$this->logger->info( |
|
527
|
|
|
'Counting ' . $field, |
|
528
|
|
|
[ |
|
529
|
|
|
'filter' => [ |
|
530
|
|
|
$field => ['$gt' => $min, '$lte' => $max] |
|
531
|
|
|
], |
|
532
|
|
|
'options' => $options, |
|
533
|
|
|
'col' => $this->name, |
|
534
|
|
|
] |
|
535
|
|
|
); |
|
536
|
|
|
|
|
537
|
|
|
$result = $this->collection->count( |
|
538
|
|
|
$query, |
|
539
|
|
|
[ |
|
540
|
|
|
Query::NO_CURSOR_TIMEOUT, |
|
541
|
|
|
Query::PROJECTION => [ |
|
542
|
|
|
$this->getIdField() => true |
|
543
|
|
|
] |
|
544
|
|
|
] |
|
545
|
|
|
); |
|
546
|
|
|
|
|
547
|
|
|
return $result; |
|
548
|
|
|
} |
|
549
|
|
|
|
|
550
|
|
|
/** |
|
551
|
|
|
* Return the max value for a field in a collection |
|
552
|
|
|
* |
|
553
|
|
|
* @param $field |
|
554
|
|
|
* @param array $query |
|
555
|
|
|
* @param array $options |
|
556
|
|
|
* @return mixed |
|
557
|
|
|
*/ |
|
558
|
1 |
View Code Duplication |
public function max($field, array $query = [], array $options = ['maxTimeMS' => 0]) |
|
559
|
|
|
{ |
|
560
|
1 |
|
$this->validateQueryOptions($options); |
|
561
|
|
|
|
|
562
|
1 |
|
$this->logger->info( |
|
563
|
1 |
|
sprintf('Maximum value in field %s', $field), |
|
564
|
|
|
[ |
|
565
|
1 |
|
'filter' => $query, |
|
566
|
|
|
'options' => $options |
|
567
|
1 |
|
] |
|
568
|
1 |
|
); |
|
569
|
|
|
|
|
570
|
1 |
|
$result = $this->findBy( |
|
571
|
1 |
|
$query, |
|
572
|
|
|
[ |
|
573
|
1 |
|
Query::PROJECTION => [ |
|
574
|
|
|
$field => true |
|
575
|
1 |
|
], |
|
576
|
1 |
|
Query::SORT => [ |
|
577
|
|
|
$field => -1 |
|
578
|
1 |
|
], |
|
579
|
1 |
|
Query::LIMIT => 1 |
|
580
|
1 |
|
] |
|
581
|
1 |
|
)->toArray(); |
|
582
|
|
|
|
|
583
|
1 |
|
return ArrayAccessor::dget($result, '0.' . $field); |
|
584
|
|
|
|
|
585
|
|
|
} |
|
586
|
|
|
|
|
587
|
|
|
/** |
|
588
|
|
|
* Return the min value for a field in a collection |
|
589
|
|
|
* |
|
590
|
|
|
* @param $field |
|
591
|
|
|
* @param array $query |
|
592
|
|
|
* @param array $options |
|
593
|
|
|
* @return \Traversable |
|
594
|
|
|
*/ |
|
595
|
1 |
View Code Duplication |
public function min($field, array $query = [], array $options = ['maxTimeMS' => 0]) |
|
596
|
|
|
{ |
|
597
|
1 |
|
$this->validateQueryOptions($options); |
|
598
|
|
|
|
|
599
|
1 |
|
$this->logger->info( |
|
600
|
1 |
|
sprintf('Minimum value in field %s', $field), |
|
601
|
|
|
[ |
|
602
|
1 |
|
'filter' => $query, |
|
603
|
|
|
'options' => $options |
|
604
|
1 |
|
] |
|
605
|
1 |
|
); |
|
606
|
|
|
|
|
607
|
1 |
|
$result = $this->findBy( |
|
608
|
1 |
|
$query, |
|
609
|
|
|
[ |
|
610
|
1 |
|
Query::PROJECTION => [ |
|
611
|
|
|
$field => true |
|
612
|
1 |
|
], |
|
613
|
1 |
|
Query::SORT => [ |
|
614
|
|
|
$field => 1 |
|
615
|
1 |
|
], |
|
616
|
1 |
|
Query::LIMIT => 1 |
|
617
|
1 |
|
] |
|
618
|
1 |
|
)->toArray(); |
|
619
|
|
|
|
|
620
|
1 |
|
return ArrayAccessor::dget($result, '0.' . $field); |
|
621
|
|
|
} |
|
622
|
|
|
|
|
623
|
|
|
/** |
|
624
|
|
|
* @param $field |
|
625
|
|
|
* @param array $filter |
|
626
|
|
|
* @param array $options |
|
627
|
|
|
* @return \mixed[] |
|
628
|
|
|
*/ |
|
629
|
|
|
public function distinct($field, array $filter = [], array $options = ['maxTimeMS' => 0]) |
|
630
|
|
|
{ |
|
631
|
|
|
|
|
632
|
|
|
$this->logger->info( |
|
633
|
|
|
'Distinct over ' . $field, |
|
634
|
|
|
[ |
|
635
|
|
|
'filter' => $filter, |
|
636
|
|
|
'options' => $options, |
|
637
|
|
|
'col' => $this->name, |
|
638
|
|
|
|
|
639
|
|
|
] |
|
640
|
|
|
); |
|
641
|
|
|
|
|
642
|
|
|
return $this->collection->distinct($field, $filter, $options); |
|
643
|
|
|
} |
|
644
|
|
|
|
|
645
|
|
|
/** |
|
646
|
|
|
* @param array $filter |
|
647
|
|
|
* @param array $options |
|
648
|
|
|
* @return int |
|
649
|
|
|
*/ |
|
650
|
8 |
|
public function count(array $filter = [], array $options = ['maxTimeMS' => 0]) |
|
651
|
|
|
{ |
|
652
|
|
|
|
|
653
|
8 |
|
$this->logger->info( |
|
654
|
8 |
|
'Counting ', |
|
655
|
|
|
[ |
|
656
|
8 |
|
'filter' => $filter, |
|
657
|
8 |
|
'options' => $options, |
|
658
|
8 |
|
'col' => $this->name, |
|
659
|
|
|
] |
|
660
|
8 |
|
); |
|
661
|
|
|
|
|
662
|
8 |
|
return $this->collection->count($filter); |
|
663
|
|
|
} |
|
664
|
|
|
|
|
665
|
|
|
|
|
666
|
|
|
/** |
|
667
|
|
|
* @param array $options |
|
668
|
|
|
*/ |
|
669
|
5 |
|
private function validateQueryOptions(array $options = []) |
|
670
|
|
|
{ |
|
671
|
|
|
/** |
|
672
|
|
|
* Sort |
|
673
|
|
|
*/ |
|
674
|
5 |
View Code Duplication |
if (isset($options[Query::SORT]) && !is_array($options[Query::SORT])) { |
|
|
|
|
|
|
675
|
|
|
throw new \InvalidArgumentException(Query::SORT . ' option must be an array'); |
|
676
|
|
|
} |
|
677
|
|
|
|
|
678
|
|
|
/** |
|
679
|
|
|
* Proj |
|
680
|
|
|
*/ |
|
681
|
5 |
View Code Duplication |
if (isset($options[Query::PROJECTION]) && !is_array($options[Query::PROJECTION])) { |
|
|
|
|
|
|
682
|
|
|
throw new \InvalidArgumentException(Query::PROJECTION . ' option must be an array'); |
|
683
|
|
|
} |
|
684
|
|
|
|
|
685
|
|
|
/** |
|
686
|
|
|
* limit |
|
687
|
|
|
*/ |
|
688
|
5 |
View Code Duplication |
if (isset($options[Query::LIMIT]) && !is_integer($options[Query::LIMIT])) { |
|
|
|
|
|
|
689
|
|
|
throw new \InvalidArgumentException(Query::LIMIT . " option must be an integer"); |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
|
|
/** |
|
693
|
|
|
* offset |
|
694
|
|
|
*/ |
|
695
|
5 |
View Code Duplication |
if (isset($options[Query::OFFSET]) && !is_integer($options[Query::OFFSET])) { |
|
|
|
|
|
|
696
|
|
|
throw new \InvalidArgumentException(Query::OFFSET . ' option must be an array'); |
|
697
|
|
|
} |
|
698
|
|
|
|
|
699
|
|
|
// TODO validate other options |
|
700
|
5 |
|
} |
|
701
|
|
|
|
|
702
|
|
|
|
|
703
|
|
|
/** |
|
704
|
|
|
* @param $id |
|
705
|
|
|
* @return MongoDB\BSON\ObjectID |
|
706
|
|
|
*/ |
|
707
|
3 |
|
public static function createObjectId($id) |
|
708
|
|
|
{ |
|
709
|
3 |
|
$class = DriverClasses::ID_CLASS; |
|
710
|
|
|
|
|
711
|
3 |
|
if ($id instanceof $class) { |
|
712
|
3 |
|
return $id; |
|
713
|
|
|
} |
|
714
|
|
|
|
|
715
|
|
|
return new $class($id); |
|
716
|
|
|
} |
|
717
|
|
|
|
|
718
|
|
|
/** |
|
719
|
|
|
* @return string |
|
720
|
|
|
*/ |
|
721
|
3 |
|
public function getIdField() |
|
722
|
|
|
{ |
|
723
|
3 |
|
return $this->id_field; |
|
724
|
|
|
} |
|
725
|
|
|
|
|
726
|
|
|
/** |
|
727
|
|
|
* @param string $id_field |
|
728
|
|
|
* @return Repository |
|
729
|
|
|
*/ |
|
730
|
|
|
public function setIdField($id_field) |
|
731
|
|
|
{ |
|
732
|
|
|
$this->id_field = $id_field; |
|
733
|
|
|
|
|
734
|
|
|
return $this; |
|
735
|
|
|
} |
|
736
|
|
|
|
|
737
|
|
|
|
|
738
|
|
|
/** |
|
739
|
|
|
* @param $operations |
|
740
|
|
|
* @throws MalformedOperationException |
|
741
|
|
|
*/ |
|
742
|
1 |
|
private function validateBulkOperations($operations) |
|
743
|
|
|
{ |
|
744
|
|
|
foreach ($operations as $op) { |
|
745
|
|
|
if (count($op) > 1) { |
|
746
|
|
|
throw new MalformedOperationException(); |
|
747
|
|
|
} |
|
748
|
|
|
|
|
749
|
1 |
|
$key = array_keys($op)[0]; |
|
750
|
|
|
|
|
751
|
|
|
if (!in_array( |
|
752
|
|
|
$key, |
|
753
|
|
|
[ |
|
754
|
|
|
'insertOne', |
|
755
|
|
|
'insertMany', |
|
756
|
|
|
'updateOne', |
|
757
|
|
|
'updateMany', |
|
758
|
|
|
'deleteOne', |
|
759
|
|
|
'deleteMany' |
|
760
|
|
|
] |
|
761
|
|
|
) |
|
762
|
|
|
) { |
|
763
|
|
|
throw new MalformedOperationException(sprintf('%s is not a valid bulk operation')); |
|
764
|
|
|
} |
|
765
|
|
|
|
|
766
|
|
|
if (!is_array($op[$key])) { |
|
767
|
|
|
throw new MalformedOperationException(sprintf('%s argument must be an array')); |
|
768
|
|
|
} |
|
769
|
|
|
} |
|
770
|
|
|
} |
|
771
|
|
|
|
|
772
|
1 |
|
public function listIndexes($options = []) { |
|
773
|
|
|
|
|
774
|
1 |
|
return $this->collection->listIndexes($options); |
|
775
|
|
|
} |
|
776
|
|
|
|
|
777
|
15 |
|
public function setIndexes(array $indexes) |
|
778
|
|
|
{ |
|
779
|
15 |
|
$this->indexes = $indexes; |
|
780
|
15 |
|
} |
|
781
|
|
|
|
|
782
|
1 |
|
public function getIndexes() |
|
783
|
|
|
{ |
|
784
|
|
|
|
|
785
|
1 |
|
return $this->indexes; |
|
786
|
|
|
} |
|
787
|
|
|
|
|
788
|
1 |
|
public function getName() |
|
789
|
|
|
{ |
|
790
|
|
|
|
|
791
|
1 |
|
return $this->name; |
|
792
|
|
|
} |
|
793
|
|
|
} |
|
794
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.