Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Prisma · Pipe

take

pipe(stage) appends a manually constructed MongoDB pipeline stage when typed pipeline methods do not cover an operation. The existing row type is retained.

How it works

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.

Syntax

pipe(stage)

Parameters

NameTypeDescription
stageMongo*StageA constructed raw MongoDB pipeline stage, typically one of the exported Mongo*Stage classes.

Returns

A pipeline with the raw stage appended and the existing row type preserved.

Key points

  • Pass a constructed stage to pipe(stage) to add it to the current pipeline.
  • Raw pipeline stages use the exported MongoDB stage-class family.
  • The execution module provides a separate stage class for each MongoDB stage.
  • Import the raw stage classes from the documented execution module.
  • Derive a stage class name by wrapping the MongoDB stage name with the Mongo prefix and Stage suffix.
  • The $unwind MongoDB stage is represented by MongoUnwindStage.

Common mistakes

  • Use pipe only when the typed pipeline methods do not provide the required MongoDB operation.
  • Adding a raw stage does not change the pipeline's existing row type.

Example: Append raw MongoDB stages

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.

When to use it

Use an uncovered aggregation stage

A required MongoDB aggregation stage has no corresponding typed pipeline method.

Add a raw match stage

A pipeline needs a manually constructed match stage with a field filter.

Compose MongoDB stage classes

A pipeline needs a count or unwind stage represented by an execution-layer class.

Interview questions

QWhat does the 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.

QWhat happens to the row type after calling 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.

QHow do you identify the class for a MongoDB stage?

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*Stage
  • MongoUnwindStage
  • MongoMatchStage
  • MongoCountStage
  • MongoFieldFilter

Source: Pipeline builder reference. Checked 2026-10-16.