tespindola:
I’m in the process of upgrading the laravel-mongodb package to version 5 in order to support Laravel 12. However, I’ve encountered a couple of issues related to the automatic conversion of _id fields to id.
Problems:
rename_embedded_id_field = false has no effect
According to the documentation, setting rename_embedded_id_field to false should prevent the package from renaming _id fields to id. Unfortunately, this configuration seems to be ignored entirely. Regardless of how it’s set, the _id fields are always renamed to id.
Unexpected renaming in aggregation pipelines
When using raw queries with aggregation (like $group), the _id field is also being automatically renamed to id. This seems unintended and problematic, especially when working with grouped results. Here’s an example:
$content = Model::raw(function ($collection) {
return $collection->aggregate([
['$group' => ['_id' => ['id' => '$brand_id', 'name' => '$brand_name'], 'count' => ['$sum' => 1]]],
['$sort' => ['count' => -1]]
], ["allowDiskUse" => true]);
})->toArray();
When accessing the result:
$content['_id']; // Throws an exception: index does not exist
$content['id']; // This works instead
This forced me to go through all my aggregation queries and manually adjust the references to _id, which is both tedious and error-prone.
just want to make sure that you added this setting under config/database.php for the rename_embedded_id_field to take affect.
or if you did: \MongoDB\Connection::setRenameEmbeddedIdField(false);
(this was introduced in 5.3 btw)
1 Like