Use an uncovered aggregation stage
A required MongoDB aggregation stage has no corresponding typed pipeline method.
Prisma · Pipe
pipe(stage) appends a manually constructed MongoDB pipeline stage when typed pipeline methods do not cover an operation. The existing row type is retained.
The pipe(stage) helper appends a manually constructed MongoDB stage to a pipeline. It preserves the row type already established by the pipeline, while allowing stages represented by the Mongo*Stage classes to be used directly.
pipe(stage)| Name | Type | Description |
|---|---|---|
stage | Mongo*Stage | A constructed raw MongoDB pipeline stage, typically one of the exported Mongo*Stage classes. |
A pipeline with the raw stage appended and the existing row type preserved.
pipe(stage) to add it to the current pipeline.Mongo prefix and Stage suffix.$unwind MongoDB stage is represented by MongoUnwindStage.import { PrismaClient } from '@prisma/client'
import { MongoMatchStage, MongoCountStage, MongoFieldFilter } from '@prisma/orm-mongo/query-ast/execution'
const prisma = new PrismaClient()
const result = await prisma.user
.aggregate()
.pipe(new MongoMatchStage(new MongoFieldFilter('active', true)))
.pipe(new MongoCountStage('total'))
.run()Adds raw MongoDB pipeline stages when the typed pipeline methods do not cover the required operation.
A required MongoDB aggregation stage has no corresponding typed pipeline method.
A pipeline needs a manually constructed match stage with a field filter.
A pipeline needs a count or unwind stage represented by an execution-layer class.
pipe helper do?pipe(stage) appends a raw MongoDB pipeline stage that you construct yourself. It is intended for operations not covered by the pipeline builder's typed methods.
Follow-up: How are raw stages represented? They are instances of MongoDB stage classes exported from @prisma/orm-mongo/query-ast/execution.
pipe?The helper preserves the row type already present in the pipeline. This lets a custom stage extend the pipeline without replacing its established type information.
Follow-up: Why does that matter? Subsequent pipeline operations can continue to use the known result shape for type checking.
Stage classes follow a naming convention: Mongo, then the MongoDB stage name without its dollar sign, then Stage. For example, $unwind maps to MongoUnwindStage.
Follow-up: Which supporting helper is documented? MongoFieldFilter builds the filter accepted by a MongoMatchStage.
pipe()Mongo*StageMongoUnwindStageMongoMatchStageMongoCountStageMongoFieldFilter