Skip to content

[Feature Request] Ability to disable root-level _id and id mapping #3453

@mcnika

Description

@mcnika

Currently, mongodb/laravel-mongodb automatically maps MongoDB’s _id field to Laravel’s id attribute on Eloquent models.
This makes it difficult to use a custom id field alongside MongoDB’s native _id.

Example use case:
I want to store documents like this:
{ "_id": ObjectId("6518e9c4c6a3d1f9b3a2f1a7"), "id": "lyqxei40yhyw", "title": "Some custom HTML block" }

But right now, if I set id, it overrides the default _id mapping or causes conflicts.

Proposed solution:
Add a configuration option or model-level property to disable the automatic mapping of _id to id, so developers can:

  • Keep _id as the real MongoDB ObjectId
  • Define their own id (or any other field like customId) without collisions

Migration issue:
I am migrating from the package jenssegers/mongodb, where having both _id and id fields was possible.
Since I already have large amounts of existing data with both fields present, the current forced mapping causes serious migration issues and makes it impossible to work with my existing dataset.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions