0

我在 node.js 中使用聚合如下

collection.aggregate(
    {
        $group : {
           _id : "$id_page", 
           "count" : {$sum : 1}
        }
    }, 
    {$sort : {"count" : -1}}, 
    {$limit : 1} 
 ).limit(1).toArray(function (err, r) { ................. })

这运行正确,但我得到了这个结果

{ id: '346593403645', _id: 57a868497e07fcf75f27009c, __v: 0 }

由于_id密钥,该对象无法被利用。

是否可以以不返回_id密钥的方式使用聚合?

4

1 回答 1

0

使用 $project 并选择显示哪个字段

    collection.aggregate(
        {
            $group : {
               _id : "$id_page", 
               "count" : {$sum : 1}
            }
        }, 
        {$sort : {"count" : -1}}, 
        {$limit : 1} ,
 {$project:{count:1,_id:0}}
     )
于 2016-08-19T05:36:17.217 回答