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",
: "author",
})
.("$author")
.({
: 0,
: 1,
: "$author.name",
})
.();What is typed well today
match,group,sort,limit,skip,count,addFields- straightforward
projectandaddFieldsshapes lookupwhen thefromside is another Mongster model- field-path based workflows where MongoDB stages still map cleanly to known shapes
addFields and count
import { } from "./models";
const = await .()
.({ : { : 0 } })
.({ : { : ["$quantity", "$unitPrice"] } })
.();
const = await .()
.({ : true })
.("done")
.();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.
import { } from "./models";
type = { : string; : number };
const = await .()
.({ : { : 0 } })
.<[]>({
: {
: 0,
: { : "ALL" },
: { : ["$quantity", "$unitPrice"] },
},
})
.();explain() on the aggregate query returns MongoDB's execution plan without running the cursor.
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.
For a full method-by-method reference, see the Aggregate API.