0

I am using aggregate in node.js as follows

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

this runs correctly but I am getting this result

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

because of the _id key, the object cannot be exploited.

Is it possible to use aggregate such a way it does not return the _id key?

chridam
  • 100,957
  • 23
  • 236
  • 235
lemahdois
  • 77
  • 3
  • 11

1 Answers1

0

use $project and choose which field is display

    collection.aggregate(
        {
            $group : {
               _id : "$id_page", 
               "count" : {$sum : 1}
            }
        }, 
        {$sort : {"count" : -1}}, 
        {$limit : 1} ,
 {$project:{count:1,_id:0}}
     )
karthi
  • 880
  • 6
  • 10