Mongo group and push pushing all fields
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
$group and $push are a common MongoDB pattern when you need grouped summaries plus raw records for each group. The tricky part is deciding what to push, because pushing full documents can inflate memory use and payload size quickly. A better approach is to group deliberately, include only required fields, and add sorting or filtering before grouping.
What $group and $push Actually Do
$group collects documents by a key and computes accumulator outputs per group. $push appends one value per input document into an array in that grouped output.
A minimal pattern looks like this:
Here each result row is one customer with an orders array.
When people say "push all fields", they often mean one of two goals:
- Keep each original document in grouped output.
- Keep a curated subset of fields that represent each document.
The second option is usually safer for performance and maintenance.
Push Full Document vs Curated Object
If you truly need the complete source row, use $$ROOT.
This is convenient, but docs can become very large. In many APIs, a curated object is better:
This keeps output stable even when source documents gain new fields later.
Sort and Filter Before Grouping
$push preserves input order, so stage order matters. If you want newest orders first inside each grouped array, sort before grouping.
Without pre-sort, array order depends on upstream execution and is not safe to treat as business logic.
Add Post-Group Transformations
You can compute summary fields and shape final output with $project.
This gives an API-ready shape and avoids leaking internal _id grouping keys when not needed.
Performance Guidance for Large Groups
Grouping into arrays can hit memory limits or produce oversized documents. Keep these guardrails:
- Use
$matchearly to reduce input rows. - Push only fields consumers need.
- Consider paginated drill-down queries instead of embedding huge arrays.
- Add indexes that support your
$matchand $sortstages. - Use
allowDiskUse: truefor heavy jobs when appropriate.
If a group can grow unbounded, design for summary output in aggregation and fetch detailed rows in a second query.
Common Pitfalls
- Using
$$ROOTeverywhere and creating massive grouped documents. - Expecting deterministic order in pushed arrays without an explicit pre-group sort.
- Grouping first and filtering later, which wastes resources.
- Forgetting MongoDB document size constraints when arrays grow.
- Returning internal fields directly to clients without a projection step.
Summary
$groupplus $pushis powerful for grouped detail and summary in one pipeline.- Use
$$ROOTonly when full documents are truly required. - Prefer curated pushed objects for stable contracts and better performance.
- Sort and filter before grouping to control array order and reduce workload.
- Plan for large-group behavior with indexing, projection, and bounded payload design.
Related reading
- Mongo tries to connect automatically to port 27017localhost
- Mongo vs cassandra single point of failure
- mongod, mac os x - rlimits warning
- MongoDB - avoid downtime during import?
- Mongodb - Can I use one arbiter for many replica sets?
- Mongodb - Difference between running mongo and mongod databases
- MongoDB - No server chosen with java async driver and replica set
- MongoDB - Query on the last element of an array?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.