$group

将集合中的文档分组,可用于统计结果。

示例

集合 sales 内容如下:

db.sales.insert([{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-03-01T08:00:00Z") },{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-03-01T09:00:00Z") },{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-03-15T09:00:00Z") },{ "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : ISODate("2014-04-04T11:21:39.736Z") },{ "_id" : 5, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-04-04T21:23:13.331Z") }])

Group by Month, Day, and Year

下面的聚合操作使用 $group 将文档按月、日、年组分组, 计算平均数量以及每个组的文档数:

  1. db.sales.aggregate(
  2. [
  3. {
  4. $group : {
  5. _id : { month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, year: { $year: "$date" } },
  6. averageQuantity: { $avg: "$quantity" },
  7. count: { $sum: 1 }
  8. }
  9. }
  10. ]
  11. )

该操作返回以下结果:

  1. { "_id" : { "month" : 3, "day" : 15, "year" : 2014 }, "averageQuantity" : 10, "count" : 1 }
  2. { "_id" : { "month" : 4, "day" : 4, "year" : 2014 }, "averageQuantity" : 15, "count" : 2 }
  3. { "_id" : { "month" : 3, "day" : 1, "year" : 2014 }, averageQuantity" : 1.5, "count" : 2 }

Group by null

下面的聚合操作指定_id 等于null的空组,计算总价格和平均数量以及集合中的所有文件数:

  1. db.sales.aggregate(
  2. [
  3. {
  4. $group : {
  5. _id : null,
  6. totalPrice: { $sum: { $multiply: [ "$price", "$quantity" ] } },
  7. averageQuantity: { $avg: "$quantity" },
  8. count: { $sum: 1 }
  9. }
  10. }
  11. ]
  12. )

该操作返回以下结果:

  1. { "_id" : null, "totalPrice" : 290, "averageQuantity" : 8.6, "count" : 5 }

检索不同的值 (类似sql Distinct)

下面的聚合操作使用 $group 将item字段去重,以检索不同的项目值:

  1. db.sales.aggregate( [ { $group : { _id : "$item" } } ] )

该操作返回以下结果:

  1. { "_id" : "xyz" }
  2. { "_id" : "jkl" }
  3. { "_id" : "abc" }

类似sql语句: select _id from sales group by _id

透视数据

现有集合 books 内容如下:

db.books.insert([{ "_id" : 8751, "title" : "The Banquet", "author" : "Dante", "copies" : 2 },{ "_id" : 8752, "title" : "Divine Comedy", "author" : "Dante", "copies" : 1 },{ "_id" : 8645, "title" : "Eclogues", "author" : "Dante", "copies" : 2 },{ "_id" : 7000, "title" : "The Odyssey", "author" : "Homer", "copies" : 10 },{ "_id" : 7020, "title" : "Iliad", "author" : "Homer", "copies" : 10 },])

Group title by author

下面的聚合操作 按authors分组, 收集books中的titles

  1. db.books.aggregate(
  2. [
  3. { $group : { _id : "$author", books: { $push: "$title" } } }
  4. ]
  5. )

该操作返回以下结果:

  1. { "_id" : "Homer", "books" : [ "The Odyssey", "Iliad" ] }
  2. { "_id" : "Dante", "books" : [ "The Banquet", "Divine Comedy", "Eclogues" ] }

Group Documents by author

下面的聚合操作 按author分组,收集 $$ROOT 系统变量(代表文档自身)

  1. db.books.aggregate(
  2. [
  3. { $group : { _id : "$author", books: { $push: "$$ROOT" } } }
  4. ]
  5. )

该操作返回以下结果:

  1. {
  2. "_id" : "Homer",
  3. "books" :
  4. [
  5. { "_id" : 7000, "title" : "The Odyssey", "author" : "Homer", "copies" : 10 },
  6. { "_id" : 7020, "title" : "Iliad", "author" : "Homer", "copies" : 10 }
  7. ]
  8. }
  9. {
  10. "_id" : "Dante",
  11. "books" :
  12. [
  13. { "_id" : 8751, "title" : "The Banquet", "author" : "Dante", "copies" : 2 },
  14. { "_id" : 8752, "title" : "Divine Comedy", "author" : "Dante", "copies" : 1 },
  15. { "_id" : 8645, "title" : "Eclogues", "author" : "Dante", "copies" : 2 }
  16. ]
  17. }

SQL VS Aggregation 区别

https://docs.mongodb.com/manual/reference/sql-aggregation-comparison/