Skip to content

Commit

Permalink
Merge pull request #12553 from raphael-papazikas/feature/add-filter-e…
Browse files Browse the repository at this point in the history
…xpr-limit-option

fix(types): add limit to $filter expression
  • Loading branch information
vkarpov15 committed Oct 17, 2022
2 parents 8ea0a44 + a8f639c commit e7d418b
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
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
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 e7d418b

Please sign in to comment.