Reputation: 98
I am building a Laravel application on top of a MongoDB instance. For the ORM layer I am using the official MongoDB Driver / Eloquent Implementation.
The DB contains JSON documents that have been imported from an external service.
They all have a field called id
, that (surprise!) is the primary key of the source. The key used by MongoDB of course, is _id
.
It turns out it is next to impossible to retrieve documents by querying the id
field using the Model classes, since in countless places in the underlying Builder implementation queries to a column id
are translated to _id
.
For example: MongoDB\Laravel\Query\Builder::compileWheres()
// Compatibility with Eloquent queries that uses "id" instead of MongoDB's _id
if ($where['column'] === 'id') {
$where['column'] = '_id';
}
This causes a query for id = 10000
to be rendered like this:
> MyModel::where('id', 10000)->toMql()
= [
"find" => [
[
"_id" => 10000,
],
[
"typeMap" => [
"root" => "object",
"document" => "array",
],
],
],
]
As far as I can see, this affects all methods exposed by the Model class that can be used to retrieve models/documents. For some reason, they have not made this behavior optional.
I know I could probably rename or duplicate the id
field(s) as a workaround, but somehow I think there must be some other way.
Have you by any chance encountered this problem and come up with a workable solution that does not involve writing queries by hand or forking the package (=D)?
Upvotes: 0
Views: 58