Skip to content

Commit 86c9cf3

Browse files
jmd-mongoandf-mongodb
authored andcommitted
DOCS-14249 documents new count accumulator
1 parent e05170b commit 86c9cf3

File tree

6 files changed

+116
-1
lines changed

6 files changed

+116
-1
lines changed

source/includes/extracts-agg-operators.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,11 @@ content: |
950950
951951
- Returns an average of numerical values. Ignores non-numeric values.
952952
953+
* - :group:`$count`
954+
955+
- Returns the count of the documents in the stage. May be used in
956+
place of ``{ $sum : 1 }``.
957+
953958
* - :group:`$first`
954959
955960
- Returns a value from the first document for each group. Order

source/reference/operator/aggregation.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,7 @@ Alphabetical Listing of Expression Operators
10181018
/reference/operator/aggregation/convert
10191019
/reference/operator/aggregation/cos
10201020
/reference/operator/aggregation/cosh
1021+
/reference/operator/aggregation/count-accum
10211022
/reference/operator/aggregation/dateAdd
10221023
/reference/operator/aggregation/dateDiff
10231024
/reference/operator/aggregation/dateFromParts
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
================================
2+
$count (aggregation accumulator)
3+
================================
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
Definition
14+
----------
15+
16+
.. group:: $count
17+
18+
.. versionadded:: 5.0
19+
20+
Returns the count of the documents in the stage.
21+
22+
.. note:: Disambiguation
23+
24+
The following page describes the accumulator :group:`$count`,
25+
available only within the :pipeline:`$group` stage. For the
26+
aggregation pipeline stage, see
27+
:pipeline:`$count (aggregation stage) <$count>`.
28+
29+
Syntax
30+
------
31+
32+
:group:`$count` takes no arguments. :group:`$count` has the following
33+
syntax:
34+
35+
.. code-block:: javascript
36+
37+
{ $count: { } }
38+
39+
Behavior
40+
--------
41+
42+
:group:`$count` is functionally equivalent to using ``{ $sum : 1 }``
43+
within the :pipeline:`$group` stage.
44+
45+
.. seealso::
46+
47+
:group:`$sum`
48+
49+
50+
Example
51+
-------
52+
53+
Consider a ``sales`` collection with the following documents:
54+
55+
.. code-block:: javascript
56+
57+
db.sales.insertMany([
58+
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2021-01-01T08:00:00Z") },
59+
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2021-02-03T09:00:00Z") },
60+
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2021-02-03T09:05:00Z") },
61+
{ "_id" : 4, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2021-02-15T08:00:00Z") },
62+
{ "_id" : 5, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2021-02-15T09:05:00Z") },
63+
{ "_id" : 6, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2021-02-15T12:05:10Z") },
64+
{ "_id" : 7, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2021-02-15T14:12:12Z") },
65+
{ "_id" : 8, "item" : "abc", "price" : 10, "quantity" : 5, "date" : ISODate("2021-03-16T20:20:13Z") }
66+
])
67+
68+
This operation returns a count of the documents in the stage.
69+
70+
.. code-block:: javascript
71+
72+
db.sales.aggregate(
73+
[
74+
{
75+
$group:
76+
{
77+
_id: null,
78+
count: { $count: { } }
79+
}
80+
}
81+
]
82+
)
83+
84+
85+
The operation returns the following results:
86+
87+
.. code-block:: javascript
88+
89+
{ "_id" : null, "count" : 8 }

source/reference/operator/aggregation/group.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ to count the number of documents in the ``sales`` collection:
137137
{
138138
$group: {
139139
_id: null,
140-
count: { $sum: 1 }
140+
count: { $count: { } }
141141
}
142142
}
143143
] )
@@ -158,6 +158,7 @@ This aggregation operation is equivalent to the following SQL statement:
158158
.. seealso::
159159

160160
- :pipeline:`$count`
161+
- :group:`$count (aggregation accumulator) <$count>`
161162

162163
.. _aggregation-group-distinct-values:
163164

source/reference/operator/aggregation/sum.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ The operation returns:
162162
{ "_id" : { "day" : 34, "year" : 2014 }, "totalAmount" : 0, "count" : 2 }
163163
{ "_id" : { "day" : 1, "year" : 2014 }, "totalAmount" : 0, "count" : 1 }
164164

165+
The :group:`$count` aggregation accumulator can be used in place of
166+
``{ $sum : 1 }`` in the :pipeline:`$group` stage.
167+
168+
.. seealso::
169+
170+
:group:`$count (aggregation accumulator) <$count>`
171+
165172
Use in ``$project`` Stage
166173
~~~~~~~~~~~~~~~~~~~~~~~~~
167174

source/release-notes/5.0.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ MongoDB 5.0 introduces the following aggregation operators:
4747
between 0 and 1 each time it is called. The new
4848
:expression:`$sampleRate` operator is based on ``$rand``. (Also
4949
added to MongoDB 4.4.2)
50+
51+
* - :group:`$count`
52+
- MongoDB 5.0 adds the
53+
:group:`$count (aggregation accumulator) <$count>` which
54+
provides a count of all documents when used in a
55+
:pipeline:`$group (aggregation) <$group>` pipeline stage.
56+
57+
.. note:: Disambiguation
58+
59+
The :group:`$count (aggregation accumulator) <$count>` is
60+
distinct from the :pipeline:`$count (aggregation) <$count>`
61+
pipeline stage.
5062

5163
General Aggregation Improvements
5264
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)