How to aggregate on huge array in mongoDB?

前端 未结 1 1911
梦毁少年i
梦毁少年i 2020-12-30 03:14

I have a mongodb of about 400gb. The documents contain a variety of fields, but the key here is an array of IDs.

So a json file might look like this

         


        
相关标签:
1条回答
  • 2020-12-30 03:48

    Try this:

    db.users.aggregate( 
     [ 
      { $unwind : "$key" }, 
      { $group : { _id : "$key", number : { $sum : 1 } } },
      { $sort : { number : -1 } }, 
      { $limit : 10000 },
      { $out:"result"},
     ], {
      allowDiskUse:true,
      cursor:{}
     }
    );
    

    Then find result by db.result.find().

    0 讨论(0)
提交回复
热议问题