From a8f639c4d6d2f05231855fd819f8f41018f58ddf Mon Sep 17 00:00:00 2001 From: Raphael Papazikas Date: Sat, 15 Oct 2022 01:13:57 +0200 Subject: [PATCH] feat(aggregation): add limit to $filter expression --- test/types/expressions.test.ts | 23 +++++++++++++++++++++++ types/expressions.d.ts | 10 ++++++++++ 2 files changed, 33 insertions(+) diff --git a/test/types/expressions.test.ts b/test/types/expressions.test.ts index f2dbdd0e2b7..c41807c53f0 100644 --- a/test/types/expressions.test.ts +++ b/test/types/expressions.test.ts @@ -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: [ diff --git a/types/expressions.d.ts b/types/expressions.d.ts index c2d83ed87dd..ecd7b3e6b79 100644 --- a/types/expressions.d.ts +++ b/types/expressions.d.ts @@ -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; } }