Skip to content

ODM — Explained with Examples

DodaTech Updated Jun 15, 2026 2 min read

ODM (Object-Document Mapping) maps JSON-like documents in NoSQL databases to programming language objects, similar to how ORMs map relational tables.

ODM stands for Object-Document Mapping. While ORM bridges object-oriented code with relational databases (tables, rows), ODM bridges it with document databases like MongoDB, CouchDB, and Firebase Firestore. Popular ODMs include Mongoose (Node.js/MongoDB), Doctrine MongoDB (PHP), and Beanie (Python/MongoDB).

Why ODM Matters

Document databases store data as nested JSON-like structures, not flat tables with joins. ODMs provide schema validation, relationship handling (embedding vs referencing), query building, and middleware hooks (e.g., hash a password before saving) that make working with document databases productive and type-safe.

Real-World Analogy

A filing cabinet (relational) vs a portfolio folder (document). An ORM helps you file and retrieve individual sheets across many cabinets. An ODM helps you manage whole folders where related information is kept together in one document. Both organize information, but the structure differs.

Example: ODM with Mongoose

const mongoose = require('mongoose');

// Define schema — validates document structure
const userSchema = new mongoose.Schema({
    name: { type: String, required: true },
    email: { type: String, required: true, unique: true },
    address: {
        street: String,
        city: String,
        zip: String
    },
    createdAt: { type: Date, default: Date.now }
});

const User = mongoose.model('User', userSchema);

// Create — document stored as a JSON-like object
const user = new User({
    name: "Alice",
    email: "alice@example.com",
    address: { street: "123 Main St", city: "Springfield", zip: "12345" }
});

user.save().then(() => console.log("User saved"));

// Read — returns a JavaScript object
User.findOne({ name: "Alice" }).then(user => {
    console.log(user.address.city);  // Output: Springfield
});

Related Terms

ORM, CRUD, Repository Pattern

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro