Scrivio Interview library
LeadershipAdvanced6 min read · updated 12 November 2026

Teams treat MongoDB as schemaless and every service writes its own variations into shared collections. How do you introduce schema governance without slowing teams down?

We give every collection one owning service, so other services read through that owner's API or events instead of writing into the collection. Each owner keeps a JSON Schema for its collection in its repository, applied as a validator, and changes it through reviewed migrations with compatibility rules for readers of older versions. We roll out with validationAction warn to find violations before enforcing, and we measure the warnings and malformed-data incidents to show that governance is paying for itself.

Why this question is asked

Schemaless is one of the most misunderstood words in our field. MongoDB lets each document have its own shape, but our code still expects fields to exist with certain types. When several services write their own variations into shared collections, every reader has to guess, and bugs appear far from the code that caused them. Interviewers ask this leadership question to see whether we can bring order without becoming a bottleneck that teams route around. In this answer, we will look at why shared collections cause the trouble, how ownership fixes most of it, how to keep schemas with the code, how to change them safely, and how to roll it out and measure it.

1Why Shared Collections Cause the Trouble

Imagine an orders collection written by checkout, by a returns service and by a batch import. Checkout stores total as a decimal, the import stores it as a string, and returns adds a refund sub-document that nobody else knows about. A reporting service reading the collection now has to handle three shapes, and a fix in one writer can break another writer's assumptions.

The problem is not MongoDB's flexibility. The problem is that nobody owns the shape. When a collection has several writers, any change needs agreement from all of them, so in practice nobody asks and the shapes drift. A schema exists anyway; it is just scattered across every service's code, undocumented and inconsistent.

2One Owner per Collection

The first and most effective rule is that each collection has exactly one owning service, and only that service writes to it. Other services that need the data call the owner's API or consume the events it publishes. This is the same database-per-service principle used in microservices, applied at the collection level.

Ownership turns a coordination problem into a local decision. The owning team can change the shape because it controls the only writer, and it publishes a contract for everyone else. Teams are not slowed down; they are freed from negotiating with every other writer.

Before and after ownership
QuestionShared collectionOwned collection
Who can write orders?checkout, returns, import joborder service only
Who decides the shape?nobody, so everybodythe order team, in its repository
How do others get data?read the collection directlyAPI or published events
What a shape change needsagreement from every writera reviewed migration in one service
Ownership is the step that makes the rest possible.

3Keeping the Schema With the Code

Each owning service keeps a JSON Schema for its collection in its own repository, next to the code that writes it. A migration applies it as the collection's validator, so the database itself rejects a malformed write, even from a script someone runs by hand. The schema is reviewed in pull requests like any other code, which gives the team a natural moment to think about compatibility.

YAML
// orders.schema.json, applied by the service's migration
{ "bsonType": "object",
"required": ["customerId", "total", "status", "schemaVersion"],
"properties": {
"total": { "bsonType": "decimal" },
"status": { "enum": ["OPEN", "PAID", "SHIPPED", "CANCELLED"] },
"schemaVersion": { "bsonType": "int", "minimum": 1 }
} }

Notice that we do not need to describe every field to get most of the value. Requiring the fields that consumers depend on and fixing their types catches the expensive mistakes, and teams can tighten the schema over time.

4Changing a Schema Safely

Governance is mostly about change. We write simple compatibility rules that every migration must follow. Adding an optional field is always allowed. Renaming, removing or changing the type of a field is a breaking change, which must go through an expand and contract sequence: add the new field, write both, migrate old documents, move readers, and only then remove the old field. A schemaVersion field in each document lets readers handle older shapes during that period.

Migrations run through a tool such as Mongock in the owning service, so they are versioned, recorded and run the same way in every environment. A platform team can provide a shared template and a pipeline check that fails a build when a migration changes a required field without the expand step, so the rules are enforced by tooling rather than by meetings.

Step throughA breaking change, done without breaking anyone
  1. Add the new field as optionalvalidator allows both
  2. Write old and new fieldsdeploy writers
  3. Backfill old documentsthrottled job
  4. Move readers to the new fieldconsumers and reports
  5. Remove the old field and tighten the validatorcontract step

5 steps in order. Play them, or step through them one at a time.

5Rolling Out Without Slowing Teams Down

We introduce validators with validationAction warn first. MongoDB accepts every write but logs the ones that would fail, so each team sees exactly which code paths write bad data without any production failures. After a few weeks of fixing, the team switches to error for that collection. We start with the collections that cause the most incidents and let early teams show the benefit.

We also make the right path the easy path: a starter library that applies schemas from the repository, a schema template, a dashboard of warnings per collection. The goal is that adding governance to a service takes an afternoon, not a quarter.

6Measuring Whether It Works

To keep support for the program, we measure it. We count validation warnings per collection from the server logs and watch them fall as teams fix writers. We track incidents caused by malformed data, and the time analytics teams spend cleaning shapes. A concrete goal helps, for example: every collection in the top 20 by traffic has one owner and an enforced validator by the end of the next two quarters, and malformed-data incidents drop by half.

7Follow-up questions

What if two services genuinely need to write the same data?

Usually one of them owns the data and the other asks it to make the change, through an API call or a command event. If both truly own parts of a document, that is a sign the document should be split into two collections, each with its own owner, linked by id.

Doesn't a validator make MongoDB as rigid as a relational database?

Not really. We choose how much to describe, optional fields still vary freely, and changing a validator is a quick collMod, not a table rebuild. The difference is that flexibility becomes a deliberate choice rather than an accident.

Who should own the overall rules?

A small platform or architecture group writes the compatibility rules and the tooling, but each service team owns its own schemas. Central review of every change would become a bottleneck; central rules plus automated checks scale much better.

8Conclusion

In this answer, we saw that schemaless collections still have a schema, scattered across every writer, and that shared collections are the root of the drift. We gave each collection one owning service, kept a JSON Schema in that service's repository applied as a validator, ran changes through reviewed migrations with expand and contract rules for compatibility, rolled validators out with warn before error, and measured warnings and malformed-data incidents to show progress.

What to say in the interviewAbout 80 seconds aloud

Open with

My starting point is that the schema exists anyway; it is just spread across every service that writes the collection, so I would make it explicit and give it an owner.

Then, if they want more

The first rule is one owning service per collection: only the owner writes, and other services read through its API or its events. That alone removes most of the drift, and it actually speeds teams up because shape changes no longer need agreement from every writer.

Each owner keeps a JSON Schema for its collection in its own repository and applies it as a validator through a migration tool like Mongock, so changes are versioned and reviewed in pull requests.

I would set simple compatibility rules: adding optional fields is always fine, while renames, removals and type changes go through expand and contract with a schemaVersion field so readers can handle older documents meanwhile, ideally checked automatically in the pipeline.

For rollout I use validationAction warn first, so teams see which code paths write bad data without breaking production, then switch to error collection by collection, starting with the ones that cause the most incidents. I would provide templates so adoption takes an afternoon, and measure validation warnings and malformed-data incidents to show the program works.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
MongoDB manual
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Teams treat MongoDB as schemaless and every service writes its own variations into...": https://scrivio-interview-library.netlify.app/databases/mongodb/955958f471/