diff --git a/packages/compass-aggregations/src/components/aggregation-side-panel/stage-wizard-use-cases/group/group-with-statistics.spec.tsx b/packages/compass-aggregations/src/components/aggregation-side-panel/stage-wizard-use-cases/group/group-with-statistics.spec.tsx index 8a9150ebf50..1a06d8e9b82 100644 --- a/packages/compass-aggregations/src/components/aggregation-side-panel/stage-wizard-use-cases/group/group-with-statistics.spec.tsx +++ b/packages/compass-aggregations/src/components/aggregation-side-panel/stage-wizard-use-cases/group/group-with-statistics.spec.tsx @@ -150,5 +150,33 @@ describe('group with statistics', function () { expect(onChange.lastCall.args[1]).to.be.null; }); }); + + context('$count', function () { + it('adds a "count" field with the $count accumulator to the generated stage', function () { + setSelectValue(/select accumulator/i, 'count'); + expect(onChange.lastCall.args[0]).to.equal( + JSON.stringify({ + _id: null, + count: { $count: {} }, + }) + ); + }); + + it('clears the field selection', function () { + setSelectValue(/select accumulator/i, 'sum'); + setComboboxValue(new RegExp(SINGLE_SELECT_LABEL, 'i'), 'orders'); + setSelectValue(/select accumulator/i, 'count'); + + // re-select sum, we expect the field to be gone, and the new accumulator to + // be invalid (not present in the result) + setSelectValue(/select accumulator/i, 'sum'); + + expect(onChange.lastCall.args[0]).to.equal( + JSON.stringify({ + _id: null, + }) + ); + }); + }); }); }); diff --git a/packages/compass-aggregations/src/components/aggregation-side-panel/stage-wizard-use-cases/group/group-with-statistics.tsx b/packages/compass-aggregations/src/components/aggregation-side-panel/stage-wizard-use-cases/group/group-with-statistics.tsx index e14c34f0a60..3492bb64635 100644 --- a/packages/compass-aggregations/src/components/aggregation-side-panel/stage-wizard-use-cases/group/group-with-statistics.tsx +++ b/packages/compass-aggregations/src/components/aggregation-side-panel/stage-wizard-use-cases/group/group-with-statistics.tsx @@ -90,6 +90,10 @@ type GroupWithStatisticsFormData = { const _getGroupAccumulatorKey = ({ field, accumulator }: GroupAccumulators) => { // we will always prepend an accumulator to the key as user // can choose to calculate values of the same field in a document. + if (accumulator === '$count') { + return 'count'; + } + const prefix = accumulator.replace(/\$/g, ''); const propertyName = mapFieldToPropertyName(field); const underscore = propertyName.startsWith('_') ? '' : '_'; @@ -110,7 +114,7 @@ const mapGroupFormStateToStageValue = ( ): Document => { const values = Object.fromEntries( data.groupAccumulators - .filter((x) => x.accumulator && x.field) + .filter((x) => x.accumulator && (x.accumulator === '$count' || x.field)) .map((x) => [_getGroupAccumulatorKey(x), _getGroupAccumulatorValue(x)]) ); return { @@ -138,8 +142,13 @@ const GroupAccumulatorForm = ({ if (!value) { return; } + const newData = [...data]; newData[index][key] = value; + if (key === 'accumulator' && value === '$count') { + newData[index].field = ''; + } + onChange(newData); }; @@ -203,15 +212,19 @@ const GroupAccumulatorForm = ({ ); })} -
of -