@@ -30,6 +30,7 @@ class QueryBuilder
3030 private $ lookup = [];
3131 private $ unwind = [];
3232 private $ group = [];
33+ private $ indexBy = null ;
3334
3435 private $ expectedMultipleResults = true ;
3536
@@ -161,12 +162,14 @@ public function select($fields): self
161162 return true ;
162163 });
163164
164- if (count ($ this ->fields ))
165+ if (count ($ this ->fields )) {
165166 $ this ->fields = call_user_func_array ("array_merge " , $ this ->fields );
167+ }
166168
167169 // Exclude built in _id if not set in fields - MongoDB always returns this by default
168- if (!in_array ("_id " , $ fields ))
170+ if (!in_array ("_id " , $ fields )) {
169171 $ this ->fields ["_id " ] = 0 ;
172+ }
170173
171174 return $ this ;
172175 }
@@ -467,16 +470,22 @@ protected function deserializeResult($array = "array", $document = "array", $roo
467470
468471 if ($ this ->deserializeMongoIds && array_key_exists ("_id " , $ this ->fields ) && $ this ->fields ["_id " ] === 1 ) {
469472 $ results = \array_map (function ($ result ) {
470- if (is_object ($ result ))
473+ if (is_object ($ result )) {
471474 $ result ->_id = (string )$ result ->_id ;
472- else
475+ }
476+ else {
473477 $ result ["_id " ] = (string )$ result ["_id " ];
478+ }
474479 return $ result ;
475480 }, $ results );
476481 }
477482
478- if ($ this ->expectedMultipleResults )
483+ if ($ this ->expectedMultipleResults ) {
484+ if ($ this ->indexBy !== null ) {
485+ $ results = array_combine (array_column ($ results , $ this ->indexBy ), $ results );
486+ }
479487 return $ results ;
488+ }
480489
481490 return empty ($ results ) ? null : $ results [0 ];
482491 }
@@ -496,6 +505,20 @@ public function toJson(): string
496505 return json_encode ($ this ->toArray ());
497506 }
498507
508+ public function indexBy (string $ property )
509+ {
510+ if (!$ this ->expectedMultipleResults ) {
511+ throw new Exception ("You can only use indexBy with findAll. " );
512+ }
513+
514+ if (!empty ($ this ->fields ) && !array_key_exists ($ property , $ this ->fields )) {
515+ throw new Exception ("Property ' {$ property }' not selected. " );
516+ }
517+
518+ $ this ->indexBy = $ property ;
519+ return $ this ;
520+ }
521+
499522 /**
500523 * Takes a variable number of QueryBuilder instances and merges them into one.
501524 *
@@ -505,7 +528,6 @@ public function toJson(): string
505528 */
506529 public static function merge (QueryBuilder ...$ queryBuilderInstances ): self
507530 {
508-
509531 $ merge = [
510532 "fields " ,
511533 "filters " ,
0 commit comments