Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
5 changes: 4 additions & 1 deletion src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If your callback does not return a CursorInterface, this behavior will be disabled. Use Cursor::toArray() for example.

Model::raw(function (\MongoDB\Collection $collection): array {
    $cursor = $collection->find([], ['typeMap' => ['array' => 'bson']);

    return $cursor->toArray();
});

}

$results = $this->query->aliasIdForResult(iterator_to_array($results));

return $this->model->hydrate($results);
Expand Down
3 changes: 3 additions & 0 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
Loading