Skip to content

Conversation

khaleelrasakh
Copy link

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, 
            ],
      ],

Checklist

This is indeed required for me cause we have developed full fledged ERP Project with 20+ modules using v4 in completely Laravel. Changing all the
code is not practical

  • Add tests and ensure they pass

khaleelcloudme and others added 2 commits October 31, 2024 12:09
… results

-added option to disable converting _id in to id automatically
@khaleelrasakh khaleelrasakh requested a review from a team as a code owner October 31, 2024 06:49
@khaleelrasakh khaleelrasakh requested a review from jmikola October 31, 2024 06:49
@jmikola jmikola requested review from GromNaN and removed request for jmikola October 31, 2024 15:43
@GromNaN
Copy link
Member

GromNaN commented Nov 4, 2024

Sorry, but this option would make package maintenance very complex. We decided to make this alias automatic in order to support Laravel features that couldn't be supported before. We can't guarantee the same level of features by deactivating it. Stay with version 4 until you find a solution for renaming the fields in your database or use MongoDB library to query the database, without the Laravel Query abstraction.

@GromNaN GromNaN closed this Nov 4, 2024
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();
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants