Getting Started

Download .md

Getting Started

ZodMongo is a lightweight MongoDB ODM powered by Zod schemas. It's built on the native MongoDB driver — no Mongoose, no overhead.

Install

npm install @mauroandre/zodmongo

ZodMongo has two peer dependencies: mongodb and zod. Both are included as regular dependencies, so they're installed automatically.

Connect to MongoDB

import { connect, close } from "@mauroandre/zodmongo";

await connect("mongodb://localhost:27017", "mydb");

// ... use the database

await close();

Define a schema

Use dbSchema to create a model. It automatically includes id, createdAt, and updatedAt.

import { dbSchema } from "@mauroandre/zodmongo";
import { z } from "zod/v4";

const userSchema = dbSchema({
    name: z.string(),
    email: z.string().email(),
    age: z.number().optional().default(0),
});

type User = z.infer<typeof userSchema>;

Basic CRUD

import { save, findMany, deleteMany } from "@mauroandre/zodmongo";

// Insert
const user = userSchema.parse({ name: "Mauro", email: "[email protected]" });
await save("users", user);
console.log(user.id); // auto-assigned ObjectId string

// Update
user.name = "Mauro André";
await save("users", user);

// Find all
const users = await findMany<User>("users");

// Find with filter
const found = await findMany<User>("users", { email: "[email protected]" });

// Delete
await deleteMany("users", { email: "[email protected]" });

What's next

  • Schemas — learn about dbSchema, embeddedSchema, and the base types
  • Queries — findMany with match filters and aggregation pipelines
  • Pagination — built-in paginated queries
  • Relations — declare references between collections