khaleelrasakh/laravel-mongodb
A Laravel package by khaleelrasakh that aims to add MongoDB support, letting you work with document-based NoSQL storage through familiar Eloquent-style models and query builders. A lightweight option for Laravel apps needing MongoDB; early-stage with limited adoption so far.
The package wires Eloquent-style models to a document store, so the fastest path is to treat it as "Eloquent with _id instead of id" and verify that assumption before writing feature code.
config/database.php (or the package's own config file) and in .env. Confirm the extension/driver is present in your PHP build before debugging anything else.create → find → update → delete round trip in php artisan tinker. Inspect the raw document in the database shell, not just the model's toArray(), so you can see exactly which key the identifier landed in.First use case to target: a single collection with a small embedded sub-document array (e.g. addresses), written and read back through the model. This exercises the identifier handling that v5.0.3 touched, without the confounding factor of relations.
Identifier handling. Historically the package auto-converted an id attribute to _id on both root documents and sub-documents. As of v5.0.3 that implicit conversion is removed. The reliable pattern now is to be explicit everywhere:
protected $primaryKey = '_id';) and let the package assign it, rather than relying on a magic id → _id mapping.id will be rewritten for you.id → _id in a form request, DTO, or model mutator. Keeping that mapping in one place is far cheaper than auditing every call site later.Model and query patterns.
where, whereIn, orderBy) as you would in Eloquent, but keep an eye on array/embedded field paths — dot notation (addresses.city) is the mental model, and it does not map cleanly onto Eloquent's relation helpers.$model->getKey() / $model->getKeyName() over hardcoding _id in application code. This keeps the code honest if the package changes again and makes the intent obvious to reviewers.Workflows and integration tips.
DB::connection('...')->collection(...) or the package's equivalent) as an escape hatch for bulk operations and diagnostics, but keep it out of domain code.The v5.0.3 change is the one to plan around. Removing automatic id → _id conversion for documents and sub-documents is a silent behavioural change, not a loud one. Code that previously worked because a nested id was rewritten will now write a literal id field alongside _id, producing documents with two identifiers. Symptoms to watch for:
id and _id present)._id returning null for records that clearly exist.Debugging approach. When an identifier-related bug appears, drop to the database shell and read the raw document first. Comparing the stored document against $model->getAttributes() localises the problem in one step: if the model has _id and the document has id, the conversion layer is the culprit; if both agree, the bug is in your query.
Config quirks.
.env and the package config; a stale cached config (php artisan config:clear) is a common cause of "my change did nothing".Extension points worth knowing.
creating, saving) are the natural place to enforce identifier normalisation centrally if you cannot touch every call site.id-shaped API to consumers while storing _id internally — useful for keeping a public JSON contract stable across this upgrade.Upgrade checklist for this release. Search the codebase for 'id' in sub-document definitions, mass-assignment arrays, and seeders; grep tests for assertions on id in nested structures; re-run your model round-trip smoke test on embedded documents specifically. Pin the version in composer.json and treat the patch number as advisory — the change described here is semantic, not cosmetic.
How can I help you explore Laravel packages today?