Mongster

Guides

Model

Think of it as a class to query your collection with

A Mongster model is the typed entry point for one collection. It combines a collection name with a schema and exposes CRUD, aggregation, hooks, and index management.

import { , ,  } from "mongster";

const  = .({
  : .().(),
  : .().(1),
}).();

const  = ("users", );

const  = .({
  : .(),
  : .().(() => new ()),
});

const  = .("audit_logs", );

await .({ : false });

const  = .();
const  = .();

Ways to create a model

  • model(name, schema) uses the default exported mongster client.
  • mongster.model(name, schema) is the same idea, but more explicit.
  • client.model(name, schema) is for custom MongsterClient instances.

The shape of the API

  • Create: insertOne, insertMany, createOne, createMany
  • Read: find, findOne, findById, count, estimatedCount, distinct
  • Update: updateOne, updateMany, findOneAndUpdate, replaceOne, findOneAndReplace, upsertOne
  • Delete: deleteOne, deleteMany, findOneAndDelete
  • Extra: aggregate, aggregateRaw, bulkWrite, syncIndexes, pre, post

insert* vs create*

Use insertOne and insertMany when you want MongoDB's raw write result. Use createOne and createMany when you want the created documents back.

Transaction-scoped models

Inside mongster.transaction(), call ctx.use(Model) to get a transaction-scoped model with the same surface area and the session already injected.

On this page