$documents
聚合管道阶段用于从一组提供的文档创建管道。 如果要在不查询集合的情况下处理特定文档,此阶段特别有用。
Syntax
{
$documents: [
<document1>,
<document2>,
...
]
}
Parameters
Parameter | Description |
---|---|
<document> |
表示要在管道中包含的文档的 JSON 对象。 |
Examples
请考虑商店集合中的这个示例文档。
{
"_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" ]
}
]
Limitations
- $documents阶段仅在数据库级聚合管道中受支持。
- 它必须是管道中的第一个阶段才能正常运行。
相关内容
- 查看有关 从 MongoDB 迁移到适用于 MongoDB 的 Azure Cosmos DB (vCore) 的选项。
- 详细了解 与 MongoDB 的功能兼容性。