Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带根的mongodb刻面_Mongodb_Aggregate_Facet - Fatal编程技术网

带根的mongodb刻面

带根的mongodb刻面,mongodb,aggregate,facet,Mongodb,Aggregate,Facet,是否有一种方法可以将根文档推送到$facet上的特定字段,而无需$group?比如说, db.artwork.aggregate( [ { $facet: { "categorizedByTags": [ { $unwind: "$tags" }, { $sortByCount: "$tags" } ], "items": [{$push

是否有一种方法可以将根文档推送到
$facet
上的特定字段,而无需
$group
?比如说,

db.artwork.aggregate( [
  {
    $facet: {
      "categorizedByTags": [
        { $unwind: "$tags" },
        { $sortByCount: "$tags" }
      ],
      "items": [{$push: "$$ROOT"}] // I want to push all root document to `items`.
    }
  }])

是的,只需使用空的
$match
,如下所示:

db.artwork.aggregate( [
  {
    $facet: {
      "categorizedByTags": [
        { $unwind: "$tags" },
        { $sortByCount: "$tags" }
      ],
      "items": [{$match: {}}]
    }
  }])
您可以这样使用:

db.artwork.aggregate( [
{
 $facet: {
   items: [ { $match: {} } ],
  }
 }
])

只需使用
“items”:[]
,它本身收集根文档。@Turivshal当我使用空数组时,mongodb引发错误。“$facet阶段不能为空。”查看工作可能是版本问题,您可以通过@Tom尝试回答