简体   繁体   English

MongoDB:如何将所有文档合并到聚合管道中的单个文档中

[英]MongoDB: How to merge all documents into a single document in an aggregation pipeline

I have the current aggregation output as follows:我的当前聚合输出如下:

[
    {
        "courseCount": 14
    },
    {
        "registeredStudentsCount": 1
    }
]

The array has two documents.该数组有两个文档。 I would like to combine all the documents into a single document having all the fields in mongoDB我想将所有文档组合成一个文档,其中包含 mongoDB 中的所有字段

{
    $group: {
      "_id": "null",
      data: {
        $push: "$$ROOT"
      }
    }
  }

When you add this as the last pipeline, it will put all the docs under data, but here data would be an array of objects.当您将其添加为最后一个管道时,它会将所有文档放在 data 下,但这里的 data 将是一个对象数组。

In your case it would be在你的情况下,它会是

{ "data":[
    {
        "courseCount": 14
    },
    {
        "registeredStudentsCount": 1
    }
] }

Another approach would be,另一种方法是,

db.collection.aggregate([
  {
    $group: {
      "_id": "null",
      f: {
        $first: "$$ROOT",
        
      },
      l: {
        $last: "$$ROOT"
      }
    }
  },
  {
    "$project": {
      "output": {
        "courseCount": "$f.courseCount",
        "registeredStudentsCount": "$l.registeredStudentsCount"
      },
      "_id": 0
    }
  }
])

It's not dynamic as first one.它不像第一个那样动态。 As you have two docs, you can use this approach.由于您有两个文档,因此您可以使用这种方法。 It outputs它输出

[
  {
    "output": {
      "courseCount": 14,
      "registeredStudentsCount": 1
    }
  }
]

With extra pipeline in the second approach在第二种方法中使用额外的管道

 {
    "$replaceRoot": {
      "newRoot": "$output"
    }
  }

You will get the output as你会得到输出为

[
  {
    "courseCount": 14,
    "registeredStudentsCount": 1
  }
]
 db.collection.aggregate([
 {
   $group: {
    _id: 0,
     merged: {
    $push: "$$ROOT"
   }
   }
  },
 {
  $replaceRoot: {
  newRoot: {
    "$mergeObjects": "$merged"
   }
  }
 }
])

Explained:解释:

  1. Group the output documents in one field with push通过 push 将输出文档分组到一个字段中
  2. Replace the document root with the merged objects用合并的对象替换文档根

Plyaground游乐场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在mongodb聚合管道中限制$ group中的文档? - How to limit documents in $group in mongodb aggregation pipeline? 如何在 MongoDB 的聚合管道中重新组合(或合并)对象? - How to regroup (or merge) objects in aggregation pipeline in MongoDB? 使用 MongoDB 聚合管道计算文档 - Counting documents with MongoDB aggregation pipeline MongoDB:Graphlookup 嵌套文档仅返回聚合中的单个文档 - MongoDB: Graphlookup nested documents only returns single document in aggregation 聚合mongodb后如何更新所有文件? - how to update all documents after aggregation mongodb? MongoDB 结合每个文档的“最大值”和所有文档的“总和”的聚合 - MongoDB Aggregation combining both 'max' of EACH document and 'sum' of ALL documents 将文档合并为单个文档 - Merge documents into single document 如何将聚合管道中的文档与MongoDB Java驱动程序3.6合并? - How to combine Documents in aggregation pipeline with MongoDB Java driver 3.6? 如何使用 Mongodb 中的聚合管道将文档数组分组到 arrays 的 arrays 中? - How to group an array of documents to arrays of arrays using the aggregation pipeline in Mongodb? MongoDB 使用聚合管道计算单个用户的所有喜欢和帖子 - MongoDB count all likes and posts for a single user using the aggregation pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM