Free tool · In your browser

MongoDB schema from JSON

Paste one or more documents and get the schema they imply: every field with its types and how often it appears, plus a $jsonSchema validator, a TypeScript interface and a Mongoose schema. Everything runs in your browser; nothing is uploaded.

  • No signup
  • 100% in your browser, nothing uploaded
  • 11 verified examples
Runs in your browser

5 documents, 20 field paths

Inferred fields

Fields inferred from 5 documents
FieldTypePresent inExample
_idobjectId100%ObjectId("6510b7f2a1c4e90012ab0001")
orderNumberstring100%A-1001
customerIdobjectId100%ObjectId("64f1a0c2e4b0a1b2c3d4e501")
statusstring100%delivered
itemsarray<object>100%
items[]objectarray elements
skustring100%KB-201
namestring100%Mechanical keyboard
qtynumber100%1
pricenumber100%6499
totalnumber100%9097
currencystring100%INR
paymentMethodstring100%upi
shippingobject100%
citystring100%Bengaluru
statestring100%Karnataka
countrystring100%IN
couponCodestring | null40%WELCOME10
createdAtdate100%2026-09-03T10:15:00Z
deliveredAtdate40%2026-09-06T14:00:00Z
db.runCommand({
  collMod: "orders",
  validator: {
    $jsonSchema: {
      bsonType: "object",
      required: [
        "_id",
        "orderNumber",
        "customerId",
        "status",
        "items",
        "total",
        "currency",
        "paymentMethod",
        "shipping",
        "createdAt"
      ],
      properties: {
        _id: { bsonType: "objectId" },
        orderNumber: { bsonType: "string" },
        customerId: { bsonType: "objectId" },
        status: { bsonType: "string" },
        items: {
          bsonType: "array",
          items: {
            bsonType: "object",
            required: ["sku", "name", "qty", "price"],
            properties: {
              sku: { bsonType: "string" },
              name: { bsonType: "string" },
              qty: { bsonType: "number" },
              price: { bsonType: "number" }
            }
          }
        },
        total: { bsonType: "number" },
        currency: { bsonType: "string" },
        paymentMethod: { bsonType: "string" },
        shipping: {
          bsonType: "object",
          required: ["city", "state", "country"],
          properties: {
            city: { bsonType: "string" },
            state: { bsonType: "string" },
            country: { bsonType: "string" }
          }
        },
        couponCode: { bsonType: ["string", "null"] },
        createdAt: { bsonType: "date" },
        deliveredAt: { bsonType: "date" }
      }
    }
  },
  validationLevel: "moderate",
  validationAction: "warn"
})

Run this on your real database, read-only

Connect MongoDB with a read-only user and wuup writes, checks and runs queries like this for you, then answers with a chart or table.

Start free

11 worked examples,
run against real data

Every example ran on the sample customers, tickets, orders, products collections before it was published, and the table under it is what MongoDB returned. Load any of them into the tool above.

Worked examples
  • aggregatecustomers01

    Field presence across a collection

    “Which top-level fields exist, and in how many documents?”

    db.customers.aggregate([
      { $project: { fields: { $objectToArray: "$$ROOT" } } },
      { $unwind: "$fields" },
      {
        $group: {
          _id: "$fields.k",
          docs: { $sum: 1 },
          types: { $addToSet: { $type: "$fields.v" } }
        }
      },
      { $sort: { _id: 1 } }
    ])
    • $objectToArray turns each document into key/value pairs.
    • Grouping by the key counts how many documents have each field and collects its BSON types.
    Result of Field presence across a collection on the sample data
    _iddocs
    _id5
    city4
    country5
    email5

    + 8 more rows

  • aggregatetickets02

    BSON types used by one field

    “What types does resolvedAt hold?”

    db.tickets.aggregate([
      { $group: { _id: { $type: "$resolvedAt" }, tickets: { $sum: 1 } } },
      { $sort: { _id: 1 } }
    ])
    • $type returns the BSON type name of a value ("date", "null", "missing", ...).
    • Grouping by it shows mixed types at a glance, here dates and nulls.
    Result of BSON types used by one field on the sample data
    _idtickets
    date2
    null3
  • findorders03

    Documents missing a field

    “Orders that have no deliveredAt field”

    db.orders.find({ deliveredAt: { $exists: false } }, { _id: 0, orderNumber: 1, status: 1 })
      .sort({ orderNumber: 1 })
    • $exists: false finds documents where the field is absent.
    • This is why the generated schema marks deliveredAt as optional (40% presence here).
    Result of Documents missing a field on the sample data
    orderNumberstatus
    A-1002shipped
    A-1004cancelled
    A-1005pending
  • findorders04

    Explicit null vs missing

    “Orders where couponCode is explicitly null (not missing)”

    db.orders.find({ couponCode: { $type: "null" } }, { _id: 0, orderNumber: 1 })
    • { $type: "null" } matches only stored nulls; { couponCode: null } would also match missing fields.
    • Telling the two apart matters when you write a validator with required fields.
    Result of Explicit null vs missing on the sample data
    orderNumber
    A-1002
  • findcustomers05

    Empty arrays

    “Customers whose tags array is empty”

    db.customers.find({ tags: { $size: 0 } }, { _id: 0, name: 1 })
    • $size: 0 matches an existing but empty array.
    • A missing tags field would not match.
    Result of Empty arrays on the sample data
    name
    Daniel Okafor
  • findproducts06

    Arrays with no first element

    “Products with no variants”

    db.products.find({ "variants.0": { $exists: false } }, { _id: 0, sku: 1 })
      .sort({ sku: 1 })
    • "variants.0" refers to the first array element; if it does not exist the array is empty or missing.
    Result of Arrays with no first element on the sample data
    sku
    CB-050
    MN-270
  • findtickets07

    Documents that fail a $jsonSchema rule

    “Tickets that would fail a validator requiring a string assignee”

    db.tickets.find({
      $nor: [
        {
          $jsonSchema: {
            required: ["assignee"],
            properties: { assignee: { bsonType: "string" } }
          }
        }
      ]
    }, { _id: 0, ticketNumber: 1 })
      .sort({ ticketNumber: 1 })
    • $jsonSchema in a find filter matches documents that pass the schema; $nor flips it.
    • Run this before adding a validator to see which existing documents would be rejected on update.
    Result of Documents that fail a $jsonSchema rule on the sample data
    ticketNumber
    T-5004
    T-5005
  • aggregatecustomers08

    Documents grouped by field count

    “How many customers have 10, 11 or 12 fields?”

    db.customers.aggregate([
      { $project: { keys: { $size: { $objectToArray: "$$ROOT" } } } },
      { $group: { _id: "$keys", customers: { $sum: 1 } } },
      { $sort: { _id: 1 } }
    ])
    • Counting keys per document is a quick test for shape drift.
    • Different counts mean optional fields; the presence column in the tool shows which ones.
    Result of Documents grouped by field count on the sample data
    _idcustomers
    101
    112
    122
  • findorders09

    Values stored with the wrong type

    “Orders whose total is not a number”

    db.orders.find({ total: { $not: { $type: "number" } } }, { _id: 0, orderNumber: 1, total: 1 })
    • The "number" alias covers int, long, double and decimal.
    • An empty result means every total is numeric, which is what the sample data shows.
    Result of Values stored with the wrong type on the sample data
    orderNumbertotal
    No documents match
  • aggregateorders10

    Distinct values for an enum

    “Every status value and how often it appears”

    db.orders.aggregate([
      { $group: { _id: "$status", orders: { $sum: 1 } } },
      { $sort: { _id: 1 } }
    ])
    • Before writing an enum in a validator or Mongoose schema, list the values that already exist.
    Result of Distinct values for an enum on the sample data
    _idorders
    cancelled1
    delivered2
    pending1
    shipped1
  • aggregatecustomers11

    Random sample for inference

    “Three random customers to paste into the schema tool”

    db.customers.aggregate([{ $sample: { size: 3 } }])
    • $sample picks random documents, a better input for schema inference than the first few by _id.
    • The result changes on every run, so there is no fixed preview.

    Result depends on when it runs ($$NOW or $sample), so no fixed preview.

How the tool works

Four steps, all inside this browser tab.

  1. 1

    Paste documents

    A JSON array, one document per line, or mongosh output with ObjectId(...) and ISODate(...).

  2. 2

    See every field

    Nested paths, array element types, mixed types and the percentage of documents that have each field.

  3. 3

    Copy a validator

    Fields present in every document become required in the $jsonSchema validator.

  4. 4

    Or copy types

    A TypeScript interface and a Mongoose schema, with optional fields marked.

What it won't do

Honest limits, so you know what to expect.

  • Upload your documents. Inference runs entirely in this page.
  • Guess from one document. Paste several (or use $sample) to see which fields are optional.
  • Enforce anything. Apply the validator with collMod yourself, after testing it.

The same guard as the product

Generated queries pass the check wuup runs on every connected database: only read stages, no server-side JavaScript, at most 30 stages.

Writes ($out, $merge)
Refused
Server JS ($where, $function)
Refused
Stages
Read-only allowlist
Your database
Never contacted
Inputs
Never leave the page

The concepts
behind the output

A short explanation of what the tool produces and the mistakes it is built to avoid.

Inferring a schema from documents

A MongoDB collection has no declared schema, but its documents do have one in practice. Looking at a sample tells you which fields always exist, which are optional, and where the same field holds different types, the usual source of bugs and empty query results.

The tool walks every document, records each field path (including inside arrays and embedded objects), counts the BSON types it sees and the share of documents that have it.

From inferred schema to validator

Fields present in 100% of the sample become required; mixed types become a bsonType list. Before applying it with db.runCommand({ collMod: ..., validator: ... }), use the $nor + $jsonSchema query from the examples below to find documents that would fail it, and start with validationAction "warn".

wuup does the same kind of scan automatically when you connect a database, so its answers use your real field names and types.

Keep going

Guides that go deeper, the product that runs these queries for you, and the other free tools.

Questions,
answered

Stop writing queries.
Start asking wuup.