更新时间:2023-09-06 19:17:46
这是有可能通过在的聚合框架 。该管道通过集合中的所有文件通过如下操作:
This is possible through the aggregation framework. The pipeline passes all documents in the collection through the following operations:
$放松
运营商 - 输出一个文档在全国生产
数组字段的每个元素通过解构它
$unwind
operator - Outputs a document for each element in the produc
array field by deconstructing it.
$匹配
运营商将仅过滤文档匹配 cod_zone
标准。
$match
operator will filter only documents that match cod_zone
criteria.
$组
运营商将通过集团指定的标识符前pression输入文件,并且使用蓄能前pression的 $推
每组:
$group
operator will group the input documents by a specified identifier expression and applies the accumulator expression $push
to each group:
$项目
运营商则重建流中的每个文件:
$project
operator then reconstructs each document in the stream:
db.collection.aggregate([
{
"$unwind": "$produc"
},
{
"$match": {
"produc.cod_zone": "08850"
}
},
{
"$group":
{
"_id": null,
"produc": {
"$push": {
"cod_prod": "$produc.cod_prod",
"description": "$produc.description",
"price" : "$produc.price",
"current_stock" : "$produc.current_stock",
"min_stock" : "$produc.min_stock",
"cod_zone" : "$produc.cod_zone"
}
}
}
},
{
"$project": {
"_id": 0,
"produc": 1
}
}
])
会产生:
{
"result" : [
{
"produc" : [
{
"cod_prod" : "0001",
"description" : "Ordenador",
"price" : 400,
"current_stock" : 3,
"min_stock" : 1,
"cod_zone" : "08850"
},
{
"cod_prod" : "0004",
"description" : "Disco Duro",
"price" : 100,
"current_stock" : 20,
"min_stock" : 5,
"cod_zone" : "08850"
},
{
"cod_prod" : "0005",
"description" : "Monitor",
"price" : 150,
"current_stock" : 0,
"min_stock" : 2,
"cod_zone" : "08850"
}
]
}
],
"ok" : 1
}