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
| Method | Returns | Notes |
|---|---|---|
find(filter?, options?) | FindQuery | Thenable builder. See Queries. |
findOne(filter, options?) | FindOneQuery | Thenable builder. See Queries. |
findById(_id, options?) | FindOneQuery | Shortcut 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
| Method | Returns |
|---|---|
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,upsertOnevalidate throughschema.parse. ThrowsQueryErroron non-array / empty-array input where applicable.findOneAndUpdate,findOneAndReplace,findOneAndDeleterequireincludeResultMetadata: trueinoptionsfor the typed return shape — see the Model guide.- For the rules Mongster applies to update operators (
$set,$inc,$push, ...), see the Updates guide. bulkWritetriggerssyncIndexes()if not yet synced and fires thebulkWritehook 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:
$setfor the body fields,$setOnInsert: { _id }when_idis present.
await .(
{ : "alice@example.com" },
{ : "alice@example.com", : "Alice" },
);Aggregation
| Method | Returns | Notes |
|---|---|---|
aggregate(options?) | AggregateQuery | Typed builder. See Aggregate. |
aggregateRaw<ReturnType>(pipeline?, options?) | Promise<ReturnType> | Escape hatch for raw pipelines. |
Extra helpers
| Method | Returns | Notes |
|---|---|---|
pre(op, fn) / post(op, fn) | this | Model-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() | string | Collection 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 } },
);
});Related references
- Queries —
FindQueryandFindOneQuery. - Aggregate —
AggregateQuery. - Schema Reference — schema-level hooks (
pre/poston a schema). - Errors —
QueryError,ValidationError,IndexSyncError.