[DOCS-14896] Investigate changes in SERVER-32739: allow limit in $filter expression Created: 26/Oct/21  Updated: 13/Nov/23  Resolved: 09/Feb/22

Status: Closed
Project: Documentation
Component/s: manual, Server
Affects Version/s: None
Fix Version/s: 5.2.0, Server_Docs_20231030, Server_Docs_20231106, Server_Docs_20231105, Server_Docs_20231113

Type: Task Priority: Major - P3
Reporter: Backlog - Core Eng Program Management Team Assignee: Jocelyn Mendez
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Documented
documents SERVER-32739 allow limit in $filter expression Closed
Participants:
Days since reply: 2 years ago
Epic Link: DOCSP-17171

 Description   
Downstream Change Summary

In 5.1 and below, $filter returns all matching array elements of its input. The new feature implemented in this ticket, allows the user to add a "limit" option to the $filter expression to restrict the number of matches returned.

User-Facing Syntax

{ $filter:

Unknown macro: { input}

}

The new syntax introduces an optional field, limit, that accepts any number expression. The limit must be a positive integer greater than zero. A limit of zero or less is rejected, as is a limit that is not an integer (e.g. NumberDouble(3.14) or the string "3"). However, integer values of non-integer types (e.g. 3.000) are supported to match the behavior of $limit. Other existing validation rules still apply: input and cond are always required, and as can be optionally specified.

To be consistent with $limit, for a limit of k, the $filter operation returns the first k elements in the order in which they appeared in the input array. If the array has fewer than k matching elements, the operation returns < k matches in order.

Description of Linked Ticket

Currently $filter expression will return all matching array elements of its input.

This is a request to add a "limit" option which caps the number of matches returned.

{$filter:{
    input:[1,2,3,10,11,12,15],
    cond:{$lt:["$$this", 13]},
    limit: 2
}}
// result
[1,2]



 Comments   
Comment by Githook User [ 09/Feb/22 ]

Author:

{'name': 'jocelyn-mendez1', 'email': '91144778+jocelyn-mendez1@users.noreply.github.com', 'username': 'jocelyn-mendez1'}

Message: DOCS-14896 $filter operator new limit field (#445)

  • DOCSP-14896 fixing //optional line up
  • DOCSP-14896 fixing //optional line up
  • DOCSP-14896 fixing //optional line up
  • DOCSP-14896 fixing //optional line up

Co-authored-by: Jocelyn Mendez <jocelyn.mendez@Jocelyns-MacBook-Pro.local>
Branch: master
https://github.com/10gen/docs-mongodb-internal/commit/cf798ff0fc73b9ef91e24b1684239aefe746641b

Comment by PM Bot [ 26/Oct/21 ]

Downstream changes updated for upstream SERVER-32739:
In 5.1 and below, $filter returns all matching array elements of its input. The new feature implemented in this ticket, allows the user to add a "limit" option to the $filter expression to restrict the number of matches returned.

User-Facing Syntax

{ $filter:

{ input: <array>, as: <string>, cond: <expression> limit: <number expression> }

}

The new syntax introduces an optional field, limit, that accepts any number expression. The limit must be a positive integer greater than zero. A limit of zero or less is rejected, as is a limit that is not an integer (e.g. NumberDouble(3.14) or the string "3"). However, integer values of non-integer types (e.g. 3.000) are supported to match the behavior of $limit. Other existing validation rules still apply: input and cond are always required, and as can be optionally specified.

To be consistent with $limit, for a limit of k, the $filter operation returns the first k elements in the order in which they appeared in the input array. If the array has fewer than k matching elements, the operation returns < k matches in order.

Generated at Thu Feb 08 08:11:28 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.