小编wuh*_*ing的帖子

我可以在MongoDB聚合查询中获得第一个文档(不是字段)吗?

聚合框架示例中,有第一个和最后一个示例:

db.zipcodes.aggregate( { $group:
                         { _id: { state: "$state", city: "$city" },
                           pop: { $sum: "$pop" } } },
                       { $sort: { pop: 1 } },
                       { $group:
                         { _id : "$_id.state",
                           biggestCity:  { $last: "$_id.city" },
                           biggestPop:   { $last: "$pop" },
                           smallestCity: { $first: "$_id.city" },
                           smallestPop:  { $first: "$pop" } } }
Run Code Online (Sandbox Code Playgroud)

我可以获得完整的zipcodes文档$first吗?

编辑:

为了澄清,我在我的快递应用程序中使用coffeescript执行以下操作,看起来很愚蠢:

@aggregate(
  {
    $group :  
      _id : "$category" 
      cnt : { $sum : 1 }
      id: {$first: "$_id"}
      name: {$first: …
Run Code Online (Sandbox Code Playgroud)

mongodb aggregation-framework

7
推荐指数
1
解决办法
4981
查看次数

标签 统计

aggregation-framework ×1

mongodb ×1