BLUENEXUS INTEGRATIONSHOW-TO

How to find schema drift in MongoDB, without writing scripts

MongoDB's flexible schema is the reason teams pick it, and the reason drift creeps in. An app release starts saving price as a string. The Android app writes promo_code while iOS writes promoCode. Old documents never had a field the new code expects. Nothing breaks on write. It breaks a week later, in a dashboard that undercounts, or a query that skips a fifth of the orders.

Finding drift means looking at every document, not a sample. An AI connected to MongoDB can run that aggregation, group each field by type, spot fields that look like the same thing under two names, and count what's affected. Fixing the data and the code stays with the backend team.

Here are four kinds of drift, three prompts, and how a delivery app catches four in one Monday check.

Four kinds of drift

Four kinds of drift

Mixed typesThe same field stored as a number in some documents and a string in others.
Two namespromoCode from one app, promo_code from another.
Missing fieldsOlder documents never had a field newer code expects.
Growing arraysAn array that keeps growing until documents get huge.

Why it hides

  • Samples miss it. Schema views that sample a thousand documents can miss a problem in one document in ten thousand.
  • Validation looks forward. MongoDB's schema validation checks inserts and updates. Documents already stored aren't checked until they change.
  • Nothing errors. A query for price > 10 silently skips the prices stored as strings.

Start with MongoDB's own tools

Compass shows a collection's schema from a sample, and Atlas flags schema problems like unbounded arrays. Schema validation can reject bad writes from now on, with warn as a gentler first step. The $type operator finds documents with the wrong type.

What's left is a full count: every field, every type, across the whole collection.

Do it in Claude or ChatGPT

Connect MongoDB with our guide to MongoDB in Claude, ChatGPT and MCP, read-only. All three prompts only read. Full scans are heavy, so run them on a secondary or analytics node.

Every field and type.

1Paste this into Claude or ChatGPT

Using MongoDB, for the [orders] collection, list every top-level field with the types it's stored as and how many documents have each type. Use an aggregation over the whole collection, not a sample, and run it on a secondary. Don't change anything.

Copy prompt

Same field, two names.

1Paste this into Claude or ChatGPT

Find fields in [orders] that look like the same thing under different names, like promoCode and promo_code. For each pair, give counts and the earliest and latest date each appears. Don't change anything.

Copy prompt

Against your schema.

1Paste this into Claude or ChatGPT

Count documents in [orders] that would fail this JSON schema: [paste schema]. Group them by which rule they break. Don't change anything.

Copy prompt

A few things to know:

  • Find when it started. Drift usually begins with a release. The date points to the code.
  • Fix the writer first. Migrating old documents is pointless while the app keeps writing new ones.
  • Then validate. Add schema validation in warn mode, then error, once the data is clean.
  • Some drift is expected. Old documents missing a new field may be fine. Decide, and write it down.

When backend, data and mobile share one collection

On your own, you'd check the collection you work on. In a company, three apps write to the same orders collection, analysts build on it, and nobody sees the whole picture until a number looks wrong.

Sparrowhawk, a 45-person grocery delivery app in Nashville, checks for drift in BlueNexus. MongoDB is connected. Every Monday at 07:00, the schema agent reads every document in the orders collection, groups fields by type and name, and prepares what needs doing for Sasha, the backend lead.

Illustration of the BlueNexus app with example data.

Illustration of the BlueNexus app with example data.

Since release 4.12, prices have been saved as strings. The Android app has always written promo_code. Old orders missing a delivery window are expected. The agent prepares the next steps for Sasha.

Sasha approves. Hollis fixes the price bug and the Android field name for release 4.13, and Drew updates two dashboards to count both names until it ships.

Illustration of the BlueNexus app with example data.

For connecting MongoDB, see MongoDB in Claude, ChatGPT and MCP. For releases, see a MongoDB index review before every release. To keep your schema in shape with your team, sign up for BlueNexus.

Questions people ask about MongoDB and AI

What is schema drift in MongoDB?

When documents in the same collection gradually differ: the same field stored with different types, under different names, or missing from older documents. MongoDB's flexible schema allows it, so it often goes unnoticed.

How do I list all fields in a MongoDB collection?

MongoDB has no built-in command for it. An aggregation that unwinds each document's keys counts every field and type across the whole collection. Tools like Compass show a sample.

Does MongoDB schema validation check existing documents?

Validation rules apply to inserts and updates. Existing documents aren't checked until they're updated, so count documents that fail the schema to see what's already there.

How do I find documents with the wrong type in MongoDB?

Use the $type operator, for example to find documents where price is a string. An aggregation can group every field by its types and count each.

Can AI fix schema drift?

It can find and count it, and suggest a migration. Running a migration on production should be a person's decision, after review.

Subscribe to our blog

Product insights, AI and product thinking delivered to your inbox.