添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Docs Menu

$filter (aggregation)

$filter

New in version 3.2 .

Selects a subset of an array to return based on the specified condition. Returns an array with only those elements that match the condition. The returned elements are in the original order.

You can use $filter for deployments hosted in the following environments:

  • MongoDB Atlas : The fully managed service for MongoDB deployments in the cloud

$filter has the following syntax:

{
$filter:
{
input: <array>,
cond: <expression>,
as: <string>,
limit: <number expression>
}
}
Field
Specification
input
An expression that resolves to an array.
cond
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 .
as
Optional. A name for the variable that represents each individual element of the input array. If no name is specified, the variable name defaults to this .
limit

Optional. 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