Mongster

Model

Typed collection API and transaction-scoped models

Models expose the collection-level API you use most often. Transaction-scoped models created through ctx.use(Model) keep the same surface, but inject a session automatically.

import { ,  } from "mongster";

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

const  = ("tasks", );

await .({ : "Write docs" });
await .({ : false }).(5);
await .({ : "Write docs" }, { : { : true } });
await .({ : true });

const  = await .().({ : true }).("done").();

Read surface

MethodReturnsNotes
find(filter?, options?)FindQueryThenable builder. See Queries.
findOne(filter, options?)FindOneQueryThenable builder. See Queries.
findById(_id, options?)FindOneQueryShortcut for { _id } filter.
count(filter?, options?)Promise<number>Uses countDocuments.
estimatedCount(options?)Promise<number>Uses MongoDB's metadata-based estimate.
distinct(key, filter?, options?)Promise<Flatten<...>[]>Distinct values for a key.

Write surface

MethodReturns
insertOne(doc, options?)InsertOneResult
insertMany(docs, options?)InsertManyResult
createOne(doc, options?)Doc | null
createMany(docs, options?)Doc[]
updateOne(filter, update, options?)UpdateResult
updateMany(filter, update, options?)UpdateResult
findOneAndUpdate(filter, update, options?)Doc | null
replaceOne(filter, replacement, options?)UpdateResult
findOneAndReplace(filter, replacement, options?)Doc | null
upsertOne(filter, doc, options?)UpdateResult
deleteOne(filter, options?)DeleteResult
deleteMany(filter, options?)DeleteResult
findOneAndDelete(filter, options?)Doc | null
bulkWrite(operations, options?)BulkWriteResult

Cross-cutting notes:

  • insertOne, insertMany, createOne, createMany, replaceOne, findOneAndReplace, upsertOne validate through schema.parse. Throws QueryError on non-array / empty-array input where applicable.
  • findOneAndUpdate, findOneAndReplace, findOneAndDelete require includeResultMetadata: true in options for the typed return shape — see the Model guide.
  • For the rules Mongster applies to update operators ($set, $inc, $push, ...), see the Updates guide.
  • bulkWrite triggers syncIndexes() if not yet synced and fires the bulkWrite hook with the operations array.

upsertOne in detail

upsertOne(filter, doc) is sugar for updateOne(filter, parsedDoc, { upsert: true }). Mongster parses the document with the schema, removes _id from the body, and emits:

  • $set for the body fields,
  • $setOnInsert: { _id } when _id is present.
await .(
  { : "alice@example.com" },
  { : "alice@example.com", : "Alice" },
);

Aggregation

MethodReturnsNotes
aggregate(options?)AggregateQueryTyped builder. See Aggregate.
aggregateRaw<ReturnType>(pipeline?, options?)Promise<ReturnType>Escape hatch for raw pipelines.

Extra helpers

MethodReturnsNotes
pre(op, fn) / post(op, fn)thisModel-level hooks. See Hooks.
syncIndexes(props?){ created, dropped, unchanged }Push schema indexes to MongoDB. { force: true } resets state, { autoDrop: false } keeps unknown DB indexes.
getCollection()Collection<Doc>Raw MongoDB collection handle.
getCollectionName()stringCollection name passed to model(...).

Transaction models

Inside mongster.transaction(async (ctx) => ...), ctx.use(Model) returns a transaction model with the same CRUD and query helpers. The difference is that every operation already passes { session }.

import {  } from "./models";

await .(async () => {
  const  = .();
  await .(
    { : "alice@example.com" },
    { : { : -50 } },
  );
});
  • QueriesFindQuery and FindOneQuery.
  • AggregateAggregateQuery.
  • Schema Reference — schema-level hooks (pre / post on a schema).
  • ErrorsQueryError, ValidationError, IndexSyncError.

On this page