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,12 @@ 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
- ],
294
- ];
295
-
296
- return new Collection ($ results );
286
+ return ['count ' => [$ wheres , []]];
297
287
} elseif ($ function == 'count ' ) {
298
288
// Translate count into sum.
299
289
$ group ['aggregate ' ] = ['$sum ' => 1 ];
@@ -348,34 +338,23 @@ public function getFresh($columns = [], $returnLazy = false)
348
338
349
339
$ options = $ this ->inheritConnectionOptions ($ options );
350
340
351
- // Execute aggregation
352
- $ results = iterator_to_array ($ this ->collection ->aggregate ($ pipeline , $ options ));
353
-
354
- // Return results
355
- return new Collection ($ results );
341
+ return ['aggregate ' => [$ pipeline , $ options ]];
356
342
} // Distinct query
357
343
elseif ($ this ->distinct ) {
358
344
// Return distinct results directly
359
- $ column = isset ($ this -> columns [0 ]) ? $ this -> columns [0 ] : '_id ' ;
345
+ $ column = isset ($ columns [0 ]) ? $ columns [0 ] : '_id ' ;
360
346
361
347
$ options = $ this ->inheritConnectionOptions ();
362
348
363
- // Execute distinct
364
- $ result = $ this ->collection ->distinct ($ column , $ wheres ?: [], $ options );
365
-
366
- return new Collection ($ result );
349
+ return ['distinct ' => [$ column , $ wheres ?: [], $ options ]];
367
350
} // Normal query
368
351
else {
369
- $ columns = [];
370
-
371
352
// Convert select columns to simple projections.
372
- foreach ($ this ->columns as $ column ) {
373
- $ columns [$ column ] = true ;
374
- }
353
+ $ projection = array_fill_keys ($ columns , true );
375
354
376
355
// Add custom projections.
377
356
if ($ this ->projections ) {
378
- $ columns = array_merge ($ columns , $ this ->projections );
357
+ $ projection = array_merge ($ projection , $ this ->projections );
379
358
}
380
359
$ options = [];
381
360
@@ -395,8 +374,8 @@ public function getFresh($columns = [], $returnLazy = false)
395
374
if ($ this ->hint ) {
396
375
$ options ['hint ' ] = $ this ->hint ;
397
376
}
398
- if ($ columns ) {
399
- $ options ['projection ' ] = $ columns ;
377
+ if ($ projection ) {
378
+ $ options ['projection ' ] = $ projection ;
400
379
}
401
380
402
381
// Fix for legacy support, converts the results to arrays instead of objects.
@@ -409,22 +388,46 @@ public function getFresh($columns = [], $returnLazy = false)
409
388
410
389
$ options = $ this ->inheritConnectionOptions ($ options );
411
390
412
- // Execute query and get MongoCursor
413
- $ cursor = $ this ->collection ->find ($ wheres , $ options );
391
+ return ['find ' => [$ wheres , $ options ]];
392
+ }
393
+ }
414
394
415
- if ($ returnLazy ) {
416
- return LazyCollection::make (function () use ($ cursor ) {
417
- foreach ($ cursor as $ item ) {
418
- yield $ item ;
419
- }
420
- });
421
- }
395
+ /**
396
+ * Execute the query as a fresh "select" statement.
397
+ *
398
+ * @param array $columns
399
+ * @param bool $returnLazy
400
+ * @return array|static[]|Collection|LazyCollection
401
+ */
402
+ public function getFresh ($ columns = [], $ returnLazy = false )
403
+ {
404
+ $ command = $ this ->toMql ($ columns );
422
405
423
- // Return results as an array with numeric keys
424
- $ results = iterator_to_array ($ cursor , false );
406
+ $ result = call_user_func_array ([$ this ->collection , key ($ command )], current ($ command ));
425
407
426
- return new Collection ($ results );
408
+ // Wrap "count" results in an array
409
+ if (is_int ($ result )) {
410
+ $ result = [
411
+ [
412
+ '_id ' => null ,
413
+ 'aggregate ' => $ result ,
414
+ ],
415
+ ];
416
+ }
417
+
418
+ if ($ returnLazy ) {
419
+ return LazyCollection::make (function () use ($ result ) {
420
+ foreach ($ result as $ item ) {
421
+ yield $ item ;
422
+ }
423
+ });
424
+ }
425
+
426
+ if ($ result instanceof \Traversable) {
427
+ $ result = iterator_to_array ($ result );
427
428
}
429
+
430
+ return new Collection ($ result );
428
431
}
429
432
430
433
/**
@@ -871,7 +874,7 @@ protected function performUpdate($query, array $options = [])
871
874
$ options = $ this ->inheritConnectionOptions ($ options );
872
875
873
876
$ wheres = $ this ->compileWheres ();
874
- $ result = $ this ->collection ->UpdateMany ($ wheres , $ query , $ options );
877
+ $ result = $ this ->collection ->updateMany ($ wheres , $ query , $ options );
875
878
if (1 == (int ) $ result ->isAcknowledged ()) {
876
879
return $ result ->getModifiedCount () ? $ result ->getModifiedCount () : $ result ->getUpsertedCount ();
877
880
}
0 commit comments