@cbnsndwch/zero-sources

project() function

Home > @cbnsndwch/zero-contracts > project

project() function

Creates a $project pipeline stage for reshaping documents.

This is equivalent to MongoDB's $project aggregation operator. Unlike the top-level projection field which applies only to the final output, $project stages can be used at any point in the pipeline.

Signature:

declare function project<T = Dict>(projection: Projection<T>): ProjectStage<T>;

Parameters

Parameter

Type

Description

projection

Projection<T>

Projection specification with field selections and computed values

Returns:

ProjectStage<T>

A $project stage

Example

// Include specific fields
const includeStage = project({
    _id: 1,
    name: 1,
    email: 1
});
// { $project: { _id: 1, name: 1, email: 1 } }

// Rename and compute fields
const computedStage = project({
    userId: '$_id',
    fullName: { $concat: ['$firstName', ' ', '$lastName'] },
    isActive: { $eq: ['$status', 'active'] }
});
// { $project: { userId: '$_id', fullName: ..., isActive: ... } }

// Complex transformations
const complexStage = project({
    compositeId: { $concat: ['$accountId', '_', '$userId'] },
    accountRef: '$accountId',
    metadata: {
        createdAt: '$createdAt',
        updatedAt: '$updatedAt'
    }
});

How was this page?