是否可以使用PHP's mongodb library执行构面聚合?
正如Mongodb's aggregation framework的文档中所述,可以使用分面搜索来创建子管道,如下所示:
db.artwork.aggregate( [
{
$facet: {
"categorizedByTags": [
{ $unwind: "$tags" },
{ $sortByCount: "$tags" }
],
"categorizedByPrice": [
// Filter out documents without a price e.g., _id: 7
{ $match: { price: { $exists: 1 } } },
{
$bucket: {
groupBy: "$price",
boundaries: [ 0, 150, 200, 300, 400 ],
default: "Other",
output: {
"count": { $sum: 1 },
"titles": { $push: "$title" }
}
}
}
],
"categorizedByYears(Auto)": [
{
$bucketAuto: {
groupBy: "$year",
buckets: 4
}
}
]
}
}
])
但是,似乎没有办法在PHP中执行此操作。我尝试过以下方法:
$cursor = $collection->aggregate([
[
'$facet' => [
'categorizedByTags' => [
'$unwind' => '$tags',
'$sort' => '$tags'
]
]
]
]);
引发以下异常:
$ facet的参数必须是数组,classifiedizedByTags是类型对象
似乎MongoDB驱动程序以某种方式处理管道,使得它似乎不可能进行构面聚合。
有没有办法让这项工作?
解决方案,感谢Veeram
$cursor = $collection->aggregate([
[
'$facet' => [
'categorizedByTags' => [
['$unwind' => '$tags'],
['$sort' => '$tags']
]
]
]
]);