On this page
  1. BSON Documents & Schema Design
  2. Contents
  3. BSON Types & ObjectId
  4. Embedding vs. Referencing
  5. Server-Side $jsonSchema Validation

BSON Documents & Schema Design

Prev: mongodb | Up: mongodb | Next: CRUD & Aggregation Pipeline
Rel: NoSQL Schema Design Patterns, JSON
Ref: MongoDB — Data Modeling
Tags: #public #databases #mongodb #nosql #bson


Contents


BSON Types & ObjectId

MongoDB stores documents in BSON (Binary JSON), extending JSON with native ObjectId (12-byte timestamp-prefixed ID), Date, Decimal128, Int64, and BinData. Single documents have a hard 16 MiB size limit — never allow unbounded arrays (e.g., infinite comments or logs) to grow inside one document.


Embedding vs. Referencing


Server-Side $jsonSchema Validation

Even in a document database, enforce contract invariants at the collection level:

db.createCollection("orders", {
  validator: {
    $jsonSchema: {
      bsonType: "object",
      required: ["userId", "status", "totalCents"],
      properties: {
        status: { enum: ["pending", "paid", "refunded"] },
        totalCents: { bsonType: "int", minimum: 0 },
      },
    },
  },
});

Linked from

Related notes