From 357d633e2c1b53f38f6ab2262ae7052fe82c9262 Mon Sep 17 00:00:00 2001 From: khaleel Date: Thu, 31 Oct 2024 12:09:16 +0530 Subject: [PATCH 1/2] -added option to disable converting bson object to array on aggregate results -added option to disable converting _id in to id automatically --- src/Eloquent/Builder.php | 5 ++++- src/Query/Builder.php | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index 4fd4880df..c134fae33 100644 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -181,7 +181,10 @@ public function raw($value = null) // Convert MongoCursor results to a collection of models. if ($results instanceof CursorInterface) { - $results->setTypeMap(['root' => 'array', 'document' => 'array', 'array' => 'array']); + if(!config('database.connections.mongodb.options.AggregateCollectionArray')){ + $results->setTypeMap(['root' => 'array', 'document' => 'array', 'array' => 'array']); + } + $results = $this->query->aliasIdForResult(iterator_to_array($results)); return $this->model->hydrate($results); diff --git a/src/Query/Builder.php b/src/Query/Builder.php index c62709ce5..273c7e03c 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -1667,6 +1667,9 @@ private function aliasIdForQuery(array $values): array */ public function aliasIdForResult(array|object $values): array|object { + if(config('database.connections.mongodb.options.DisableAliasIdForResult')){ + return $values; + } if (is_array($values)) { if (array_key_exists('_id', $values) && ! array_key_exists('id', $values)) { $values['id'] = $values['_id']; From 40cba4874940247bfb610dd52bf708f29d1b3be6 Mon Sep 17 00:00:00 2001 From: khaleelrasakh Date: Thu, 31 Oct 2024 12:14:02 +0530 Subject: [PATCH 2/2] Update README.md --- README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0619f387c..7e74fe09f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,18 @@ Laravel MongoDB =============== - +This is fork supports disabling _id to id automatic conversion and disablling returning results as arrays in aggregate instead of bson object based on database configuration. + 'mongodb' => [ + 'driver' => 'mongodb', + 'dsn' => env('DB_URI'), + 'database' => env('DB_NAME'), + 'options' => [ + + 'ssl' => true, + 'AggregateCollectionArray'=>true, //add this to disable aggregate array + 'DisableAliasIdForResult'=>true, //add this to disable automatic _id conversion + 'tlsAllowInvalidCertificates' => true, + ], + ], [![Latest Stable Version](http://img.shields.io/github/release/mongodb/laravel-mongodb.svg)](https://packagist.org/packages/mongodb/laravel-mongodb) [![Total Downloads](http://img.shields.io/packagist/dm/mongodb/laravel-mongodb.svg)](https://packagist.org/packages/mongodb/laravel-mongodb) [![Build Status](https://img.shields.io/github/actions/workflow/status/mongodb/laravel-mongodb/build-ci.yml)](https://github.com/mongodb/laravel-mongodb/actions/workflows/build-ci.yml)