Mongster

Guides

Aggregation

Type-safe aggregation is not a myth anymore

Aggregation is where Mongster tries to give you useful type help without hiding the pipeline model that MongoDB already has.

Group and summarize

import {  } from "./models";

const  = await .()
  .({ : true })
  .("$region", {
    : { : 1 },
    : { : "$total" },
  })
  .({ : -1 })
  .();

Join another collection

import { ,  } from "./models";

const  = await .()
  .({
    : ,
    : "authorId",
    : "_id",
    : "authors",
  })
  .("$authors")
  .({
    : 0,
    : 1,
    : "$authors.name",
  })
  .();

What is typed well today

  • match, group, sort, limit, skip, count
  • straightforward project and addFields shapes
  • lookup when the from side is another Mongster model
  • field-path based workflows where MongoDB stages still map cleanly to known shapes

Where to use an escape hatch

Use raw<YourType>() on AggregateQuery, or aggregateRaw<YourType>() on the model, when you need complex operators that the current inference does not model well yet.

Important limitation

lookup() accepts Mongster model instances only, not raw collection name strings. It also returns arrays, so use unwind() when you want a single joined document shape.

On this page