-
Type: Bug
-
Resolution: Duplicate
-
Priority: Major - P3
-
None
-
Affects Version/s: 5.2.0, 6.0.5
-
Component/s: None
-
None
-
Query Optimization
-
ALL
-
Grouping sum of $bsonSize gets the sum of full documents not just the projected fields.
In MongoDB version 5.0, using the sum of document sizes with a group stage as recommended on this page works when fields are projected (see steps below).
However in version 6.0, it merely returns the sum of the whole document size, disregarding any field projections. I believe this may have to do with the change in release 5.2 to using Slot Based Execution query engine to execute $group stages. In 5.0 the regular query execution engine is used vs. 6.0 the SBE is used.
What is interesting is that if you use a $unset stage to exclude fields then the size is calculated correctly. So a potential workaround is to exclude fields rather than include fields.
Another potential workaround is to use 2 stages (project document size then sum that size) rather than the 1 group stage - example shown below.
- is related to
-
SERVER-77183 $project followed by $group gives incorrect results sometimes
- Closed