你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

$过滤器

运算符 $filter 用于根据指定的条件筛选数组中的元素。 需要操作或检索文档中的特定数组元素时,此运算符非常有用。

Syntax

{
  $filter: {
    input: "<array>",
    as: "<string>",
    cond: "<expression>"
  }
}

参数

参数 Description
input 解析为数组的表达式。
as 一个字符串,指定输入数组中每个元素的变量名称。
cond 一个表达式,用于确定是否在输出数组中包含元素。

例子

请考虑商店集合中的这个示例文档。

{
    "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
    "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
    "location": {
        "lat": 60.1441,
        "lon": -141.5012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 2,
            "partTime": 0
        }
    },
    "sales": {
        "salesByCategory": [
            {
                "categoryName": "DJ Headphones",
                "totalSales": 35921
            },
            {
                "categoryName": "DJ Cables",
                "totalSales": 1000
            }
        ],
        "fullSales": 3700
    },
    "promotionEvents": [
        {
            "eventName": "Bargain Blitz Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 3,
                    "Day": 11
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 2,
                    "Day": 18
                }
            },
            "discounts": [
                {
                    "categoryName": "DJ Turntables",
                    "discountPercentage": 18
                },
                {
                    "categoryName": "DJ Mixers",
                    "discountPercentage": 15
                }
            ]
        },
        {
            "eventName": "Discount Delight Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 5,
                    "Day": 11
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 5,
                    "Day": 18
                }
            }
        }
    ],
    "tag": [
        "#ShopLocal",
        "#FashionStore",
        "#SeasonalSale",
        "#FreeShipping",
        "#MembershipDeals"
    ]
}

示例 1:检索按条件筛选的元素

此查询演示如何基于 totalSales..

db.stores.aggregate([{
        $match: {
            _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"
        }
    },
    {
        $project: {
            filteredSalesByCategory: {
                $filter: {
                    input: "$sales.salesByCategory",
                    as: "item",
                    cond: {
                        $gt: ["$$item.totalSales", 10000]
                    }
                }
            }
        }
    }
])

此查询返回以下结果。

[
  {
      "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
      "filteredSalesByCategory": [
          {
              "categoryName": "DJ Headphones",
              "totalSales": 35921
          }
      ]
  }
]