$documents

匯總 $documents 管線階段可用來從一組提供的檔建立管線。 當您想要處理特定檔而不查詢集合時,這個階段特別有用。

語法

{
  $documents: [
    <document1>,
    <document2>,
    ...
  ]
}

參數

參數 Description
<document> JSON 物件,表示要包含在管線中的檔。

範例

請參考商店集合中的此範例檔。

{
    "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
    "name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
    "location": {
        "lat": -89.2384,
        "lon": -46.4012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 8,
            "partTime": 20
        }
    },
    "sales": {
        "totalSales": 75670,
        "salesByCategory": [
            {
                "categoryName": "Wine Accessories",
                "totalSales": 34440
            },
            {
                "categoryName": "Bitters",
                "totalSales": 39496
            },
            {
                "categoryName": "Rum",
                "totalSales": 1734
            }
        ]
    },
    "promotionEvents": [
        {
            "eventName": "Unbeatable Bargain Bash",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 7
                },
                {
                    "categoryName": "Bitters",
                    "discountPercentage": 15
                },
                {
                    "categoryName": "Brandy",
                    "discountPercentage": 8
                },
                {
                    "categoryName": "Sports Drinks",
                    "discountPercentage": 22
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Steal of a Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 29
                }
            },
            "discounts": [
                {
                    "categoryName": "Organic Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "White Wine",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Sparkling Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 17
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 23
                }
            ]
        }
    ]
}

範例 1:從特定檔建立管線

此查詢示範如何使用階段來 $documents 處理一組預先定義的文件:

db.aggregate([{
    $documents: [{
        _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
        name: "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
        location: {
            lat: 60.1441,
            lon: -141.5012
        },
        sales: {
            fullSales: 3700
        },
        tag: ["#ShopLocal", "#SeasonalSale"]
    }, {
        _id: "7e53ca0f-6e24-4177-966c-fe62a11e9af5",
        name: "Contoso, Ltd. | Office Supply Deals - South Shana",
        location: {
            lat: 40.7128,
            lon: -74.0060
        },
        sales: {
            fullSales: 5400
        },
        tag: ["#TechDeals", "#FreeShipping"]
    }]
}, {
    $project: {
        _id: 1,
        name: 1,
        "location.lat": 1,
        "location.lon": 1,
        "sales.fullSales": 1,
        tags: "$tag"
    }
}])

此查詢會傳回下列結果:

[
    {
        "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
        "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
        "location": {
            "lat": 60.1441,
            "lon": -141.5012
        },
        "sales": {
            "fullSales": 3700
        },
        "tags": [
            "#ShopLocal",
            "#SeasonalSale"
        ]
    },
    {
        "_id": "7e53ca0f-6e24-4177-966c-fe62a11e9af5",
        "name": "Contoso, Ltd. | Office Supply Deals - South Shana",
        "location": {
            "lat": 40.7128,
            "lon": -74.006
        },
        "sales": {
            "fullSales": 5400
        },
        "tags": [
            "#TechDeals",
            "#FreeShipping"
        ]
    }
]

範例 2:與其他管線階段結合$documents

此查詢會結合$documents管線階段以及$match和$sort管線階段。

db.aggregate([{
    $documents: [{
        _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
        name: "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
        location: {
            lat: 60.1441,
            lon: -141.5012
        },
        sales: {
            fullSales: 3700
        },
        tag: ["#ShopLocal", "#SeasonalSale"]
    }, {
        _id: "7e53ca0f-6e24-4177-966c-fe62a11e9af5",
        name: "Contoso, Ltd. | Office Supply Deals - South Shana",
        location: {
            lat: 40.7128,
            lon: -74.0060
        },
        sales: {
            fullSales: 5400
        },
        tag: ["#TechDeals", "#FreeShipping"]
    }]
}, {
    $match: {
        "sales.fullSales": {
            $gt: 4000
        }
    }
}, {
    $sort: {
        "sales.fullSales": -1
    }
}])

此查詢會傳回下列結果:

[
  {
    "_id": "7e53ca0f-6e24-4177-966c-fe62a11e9af5",
    "name": "Contoso, Ltd. | Office Supply Deals - South Shana",
    "location": { "lat": 40.7128, "lon": -74.006 },
    "sales": { "fullSales": 5400 },
    "tag": [ "#TechDeals", "#FreeShipping" ]
  }
]

局限性

  • 資料庫層級匯總管線僅支援$documents階段。
  • 它必須是管線中的第一個階段,才能正常運作。