15
15
use MongoDB \BSON \ObjectID ;
16
16
use MongoDB \BSON \Regex ;
17
17
use MongoDB \BSON \UTCDateTime ;
18
+ use MongoDB \Driver \Cursor ;
18
19
use RuntimeException ;
19
20
20
21
/**
@@ -215,27 +216,25 @@ public function cursor($columns = [])
215
216
}
216
217
217
218
/**
218
- * Execute the query as a fresh "select" statement .
219
+ * Return the Mongo Query to be run in the form of a 1 element array like ['method' => [arguments]] .
219
220
*
220
- * @param array $columns
221
- * @param bool $returnLazy
222
- * @return array|static[]|Collection|LazyCollection
221
+ * @param $columns
222
+ * @return array<string, mixed[]>
223
223
*/
224
- public function getFresh ($ columns = [], $ returnLazy = false )
224
+ public function toMql ($ columns = []): array
225
225
{
226
226
// If no columns have been specified for the select statement, we will set them
227
227
// here to either the passed columns, or the standard default of retrieving
228
228
// all of the columns on the table using the "wildcard" column character.
229
- if ($ this ->columns = == null ) {
230
- $ this -> columns = $ columns ;
229
+ if ($ this ->columns ! == null ) {
230
+ $ columns = $ this -> columns ;
231
231
}
232
232
233
233
// Drop all columns if * is present, MongoDB does not work this way.
234
- if (in_array ('* ' , $ this -> columns )) {
235
- $ this -> columns = [];
234
+ if (in_array ('* ' , $ columns )) {
235
+ $ columns = [];
236
236
}
237
237
238
- // Compile wheres
239
238
$ wheres = $ this ->compileWheres ();
240
239
241
240
// Use MongoDB's aggregation framework when using grouping or aggregation functions.
@@ -254,7 +253,7 @@ public function getFresh($columns = [], $returnLazy = false)
254
253
}
255
254
256
255
// Do the same for other columns that are selected.
257
- foreach ($ this -> columns as $ column ) {
256
+ foreach ($ columns as $ column ) {
258
257
$ key = str_replace ('. ' , '_ ' , $ column );
259
258
260
259
$ group [$ key ] = ['$last ' => '$ ' .$ column ];
@@ -279,21 +278,14 @@ public function getFresh($columns = [], $returnLazy = false)
279
278
$ aggregations = blank ($ this ->aggregate ['columns ' ]) ? [] : $ this ->aggregate ['columns ' ];
280
279
281
280
if (in_array ('* ' , $ aggregations ) && $ function == 'count ' ) {
282
- // When ORM is paginating, count doesnt need a aggregation, just a cursor operation
281
+ // When ORM is paginating, count doesn't need an aggregation, just a cursor operation
283
282
// elseif added to use this only in pagination
284
283
// https://docs.mongodb.com/manual/reference/method/cursor.count/
285
284
// count method returns int
286
285
287
- $ totalResults = $ this ->collection ->count ($ wheres );
288
- // Preserving format expected by framework
289
- $ results = [
290
- [
291
- '_id ' => null ,
292
- 'aggregate ' => $ totalResults ,
293
- ],
286
+ return [
287
+ 'count ' => [$ wheres , []],
294
288
];
295
-
296
- return new Collection ($ results );
297
289
} elseif ($ function == 'count ' ) {
298
290
// Translate count into sum.
299
291
$ group ['aggregate ' ] = ['$sum ' => 1 ];
@@ -348,34 +340,23 @@ public function getFresh($columns = [], $returnLazy = false)
348
340
349
341
$ options = $ this ->inheritConnectionOptions ($ options );
350
342
351
- // Execute aggregation
352
- $ results = iterator_to_array ($ this ->collection ->aggregate ($ pipeline , $ options ));
353
-
354
- // Return results
355
- return new Collection ($ results );
343
+ return ['aggregate ' => [$ pipeline , $ options ]];
356
344
} // Distinct query
357
345
elseif ($ this ->distinct ) {
358
346
// Return distinct results directly
359
- $ column = isset ($ this -> columns [0 ]) ? $ this -> columns [0 ] : '_id ' ;
347
+ $ column = isset ($ columns [0 ]) ? $ columns [0 ] : '_id ' ;
360
348
361
349
$ options = $ this ->inheritConnectionOptions ();
362
350
363
- // Execute distinct
364
- $ result = $ this ->collection ->distinct ($ column , $ wheres ?: [], $ options );
365
-
366
- return new Collection ($ result );
351
+ return ['distinct ' => [$ column , $ wheres ?: [], $ options ]];
367
352
} // Normal query
368
353
else {
369
- $ columns = [];
370
-
371
354
// Convert select columns to simple projections.
372
- foreach ($ this ->columns as $ column ) {
373
- $ columns [$ column ] = true ;
374
- }
355
+ $ projection = array_fill_keys ($ columns , true );
375
356
376
357
// Add custom projections.
377
358
if ($ this ->projections ) {
378
- $ columns = array_merge ($ columns , $ this ->projections );
359
+ $ projection = array_merge ($ projection , $ this ->projections );
379
360
}
380
361
$ options = [];
381
362
@@ -395,8 +376,8 @@ public function getFresh($columns = [], $returnLazy = false)
395
376
if ($ this ->hint ) {
396
377
$ options ['hint ' ] = $ this ->hint ;
397
378
}
398
- if ($ columns ) {
399
- $ options ['projection ' ] = $ columns ;
379
+ if ($ projection ) {
380
+ $ options ['projection ' ] = $ projection ;
400
381
}
401
382
402
383
// Fix for legacy support, converts the results to arrays instead of objects.
@@ -409,22 +390,46 @@ public function getFresh($columns = [], $returnLazy = false)
409
390
410
391
$ options = $ this ->inheritConnectionOptions ($ options );
411
392
412
- // Execute query and get MongoCursor
413
- $ cursor = $ this ->collection ->find ($ wheres , $ options );
393
+ return ['find ' => [$ wheres , $ options ]];
394
+ }
395
+ }
414
396
415
- if ($ returnLazy ) {
416
- return LazyCollection::make (function () use ($ cursor ) {
417
- foreach ($ cursor as $ item ) {
418
- yield $ item ;
419
- }
420
- });
421
- }
397
+ /**
398
+ * Execute the query as a fresh "select" statement.
399
+ *
400
+ * @param array $columns
401
+ * @param bool $returnLazy
402
+ * @return array|static[]|Collection|LazyCollection
403
+ */
404
+ public function getFresh ($ columns = [], $ returnLazy = false )
405
+ {
406
+ $ command = $ this ->toMql ($ columns );
422
407
423
- // Return results as an array with numeric keys
424
- $ results = iterator_to_array ($ cursor , false );
408
+ $ result = call_user_func_array ([$ this ->collection , key ($ command )], current ($ command ));
425
409
426
- return new Collection ($ results );
410
+ // Wrap "count" results in an array
411
+ if (is_int ($ result )) {
412
+ $ result = [
413
+ [
414
+ '_id ' => null ,
415
+ 'aggregate ' => $ result ,
416
+ ],
417
+ ];
418
+ }
419
+
420
+ if ($ returnLazy ) {
421
+ return LazyCollection::make (function () use ($ result ) {
422
+ foreach ($ result as $ item ) {
423
+ yield $ item ;
424
+ }
425
+ });
426
+ }
427
+
428
+ if ($ result instanceof \Traversable) {
429
+ $ result = iterator_to_array ($ result );
427
430
}
431
+
432
+ return new Collection ($ result );
428
433
}
429
434
430
435
/**
@@ -871,7 +876,7 @@ protected function performUpdate($query, array $options = [])
871
876
$ options = $ this ->inheritConnectionOptions ($ options );
872
877
873
878
$ wheres = $ this ->compileWheres ();
874
- $ result = $ this ->collection ->UpdateMany ($ wheres , $ query , $ options );
879
+ $ result = $ this ->collection ->updateMany ($ wheres , $ query , $ options );
875
880
if (1 == (int ) $ result ->isAcknowledged ()) {
876
881
return $ result ->getModifiedCount () ? $ result ->getModifiedCount () : $ result ->getUpsertedCount ();
877
882
}
0 commit comments