Advanced

Schema Analysis

Understand your MongoDB document structure with Sutido.

Overview

MongoDB is schema-flexible, meaning documents in the same collection can have different structures. Sutido helps you understand your data by analyzing document schemas.

How Schema Analysis Works

Sutido samples documents from your collection to build a picture of your schema:

  1. Documents are sampled from the collection
  2. Fields are extracted up to three levels deep
  3. Field types and frequencies are analyzed
  4. Results power the autocomplete system

Field Autocomplete

The most visible benefit of schema analysis is intelligent autocomplete. When you start typing a query, Sutido suggests field names from your actual data:

// Start typing and see suggestions
db.users.find({ st  // → suggests "status", "state", "startDate"

// Nested fields are also suggested
db.orders.find({ "items.  // → suggests "items.productId", "items.quantity"

Nested Field Detection

Sutido extracts nested paths automatically:

  • Level 1: name, email, address
  • Level 2: address.city, address.zip
  • Level 3: orders.items.productId

Note: Deeply nested paths (4+ levels) aren't automatically extracted but can still be typed manually.

Understanding Field Types

Documents in MongoDB can have mixed types for the same field. When querying, be aware of:

Common Type Variations

Scenario Example
String vs Number age: "25" vs age: 25
Date vs String created: ISODate(...) vs created: "2026-01-01"
Null vs Missing email: null vs field not present
Array vs Single tags: ["a", "b"] vs tags: "a"

Querying by Type

Use the $type operator to find documents with specific field types:

// Find documents where age is a string
db.users.find({ age: { $type: "string" } })

// Find documents where age is a number
db.users.find({ age: { $type: "number" } })

// Find documents with null values
db.users.find({ email: { $type: "null" } })

// Find documents where field is missing
db.users.find({ email: { $exists: false } })

Common Type Values

Type Number Alias
Double1"double"
String2"string"
Object3"object"
Array4"array"
Binary5"binData"
ObjectId7"objectId"
Boolean8"bool"
Date9"date"
Null10"null"
32-bit Integer16"int"
64-bit Integer18"long"
Decimal12819"decimal"

Schema Validation

MongoDB supports schema validation to enforce document structure. Check existing validation rules:

db.getCollectionInfos({ name: "users" })

Finding Schema Inconsistencies

Use aggregation to analyze field presence and types across your collection:

Check Field Presence

// Count documents with/without email field
db.users.aggregate([
  { $group: {
    _id: { hasEmail: { $ifNull: [{ $type: "$email" }, "missing"] } },
    count: { $sum: 1 }
  }}
])

Analyze Field Types

// Check types of the 'age' field
db.users.aggregate([
  { $group: {
    _id: { $type: "$age" },
    count: { $sum: 1 }
  }}
])

Best Practices

Consistent Types

Use consistent types for each field to simplify queries and indexing. Convert legacy data when possible.

Document Field Names

Maintain documentation of your expected schema, even though MongoDB doesn't enforce it.

Use Validation

For production databases, consider adding schema validation to prevent invalid documents.

Next Steps

Learn about Index Management to optimize query performance.