Menu

The MongoDB Method That Looked Tenant-Scoped but Wasn't
📰

The MongoDB Method That Looked Tenant-Scoped but Wasn't

DEV Community: backend·Athila Figueiredo·about 10 hours ago
#sHF0Bhzm
#devtobackend#devto#dev#tenant#isolation
Long article (424 words) — no AI summary yet.
Reading 0:00
15s threshold

The problem

MongoDB has no Row-Level Security. Unlike Postgres, there's no policy engine enforcing tenant isolation at the database level -- isolation is only as good as the discipline applied at the query layer, in application code. I built a repository pattern that makes the safe path the only path, and documented a real bug where one method found a way around it anyway.

What it enforces, and how

Tenant-scoped base repository : every read, write, and aggregation goes through a method that injects the tenant filter automatically. A caller has to go out of their way to query the raw collection directly.

Compound unique index design for multi-tenancy : (tenant_id, order_number) is unique together, so the same order number is free to reuse across different tenants but rejected as a duplicate within the same one.

Aggregation pipelines that cannot forget tenant scope : the aggregate wrapper always prepends a $match stage on tenant_id , so a caller cannot write a pipeline that silently scans every tenant's…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.