1

My query is taking ~3 seconds to execute with the $group stage, How can I faster this?

My query is:
db.getCollection('employee_data').aggregate([
  { $match: //Filter
  },
  { 
   $group: 
   {
     _id: {
       employee_id:"$employee_id",
       activity_date:"$activity_date"
     }, 
     trips: { $sum: 1 }
   }
  }
])
Data size : 4M records

closed as unclear what you're asking by MDCCL, John Eisbrener, Philᵀᴹ, Tom V, mustaccio May 3 at 18:19

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

0

As per MongoDB Documentation here Groups documents by some specified expression and outputs to the next stage a document for each distinct grouping. The output documents contain an _id field which contains the distinct group by key. The output documents can also contain computed fields that hold the values of some accumulator expression grouped by the $group’s _id field. $group does not order its output documents.

The $group stage has the following prototype form:

{ $group: { _id: <expression>, <field1>: { <accumulator1> : <expression1> }, ... } }

The _id field is mandatory; however, you can specify an _id value of null, or any other constant value, to calculate accumulated values for all the input documents as a whole.

$group Operator and Memory

The $group stage has a limit of 100 megabytes of RAM. By default, if the stage exceeds this limit, $group will produce an error. However, to allow for the handling of large datasets, set the allowDiskUse option to true to enable $group operations to write to temporary files.

As per blog of @VladMihalcea here the MongoDB aggregation framework is extremely useful and its performances can’t go unnoticed.that didn’t require any extra optimization, aiming to demonstrate the out-of-the-box performance of MongoDB.

For further your ref here

Not the answer you're looking for? Browse other questions tagged or ask your own question.