Skip to content

Commit

Permalink
feat(aggregation): add limit to $filter expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Papazikas committed Oct 14, 2022
1 parent 8ea0a44 commit a8f639c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
23 changes: 23 additions & 0 deletions test/types/expressions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,29 @@ const switchExpr: Expression.Switch = {
}
};

const filterExprMinimumRequiredFields: Expression.Filter = {
$filter: {
input: '$items',
cond: { $gte: ['$$item.price', 100] }
}
};

const filterExprAs: Expression.Filter = {
$filter: {
input: '$items',
as: 'items',
cond: { $gte: ['$$item.price', 100] }
}
};

const filterLimit: Expression.Filter = {
$filter: {
input: '$items',
cond: { $gte: ['$$item.price', 100] },
limit: 5
}
};

(function gh12058() {
const concat: Expression.ConcatArrays = {
$concatArrays: [
Expand Down
10 changes: 10 additions & 0 deletions types/expressions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,16 @@ declare module 'mongoose' {
* An expression that resolves to a boolean value used to determine if an element should be included in the output array. The expression references each element of the input array individually with the variable name specified in as.
*/
cond: BooleanExpression;
/**
* A number expression that restricts the number of matching array elements that $filter returns. You cannot specify a limit less than 1. The matching array elements are returned in the order they appear in the input array.
*
* If the specified limit is greater than the number of matching array elements, $filter returns all matching array elements.
* If the limit is null, $filter returns all matching array elements.
*
* @version 5.2
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/filter/#using-the-limit-field
*/
limit?: NumberExpression;
}
}

Expand Down

0 comments on commit a8f639c

Please sign in to comment.