MongoDB 聚合$count


聚合$count

此聚合阶段计算从前一阶段传递的文档总数。

示例

在此示例中,我们使用从示例数据加载的 "sample_restaurants" 数据库聚合简介部分。

db.restaurants.aggregate([
  {
    $match: { "cuisine": "Chinese" }
  },
  {
    $count: "totalChinese"
  }
])
亲自试一试 »

这将返回文档的数量$countstage 作为一个名为 "totalChinese" 的字段。