Skip to content

Commit 2302a7e

Browse files
committed
DOCS-9118: clarify sortByCount expression
1 parent d060af9 commit 2302a7e

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

source/reference/operator/aggregation/sortByCount.txt

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,39 @@ Definition
4343

4444
* - ``expression``
4545

46-
- :ref:`Expression <aggregation-expressions>` to group by. To
47-
specify a :ref:`field path <agg-quick-ref-field-paths>`,
46+
- :ref:`Expression <aggregation-expressions>` to group by. You
47+
can specify any expression except for a document literal.
48+
49+
To specify a :ref:`field path <agg-quick-ref-field-paths>`,
4850
prefix the field name with a dollar sign ``$`` and enclose it
49-
in quotes. The ``expression`` can not evaluate to an object.
50-
51+
in quotes. For example, to group by the field ``employee``,
52+
specify ``"$employee"`` as the expression.
53+
54+
.. code-block:: javascript
55+
56+
{ $sortByCount: "$employee" }
57+
58+
Although you cannot specify a document literal for the group
59+
by expression, you can, however, specify a field or an
60+
expression that evaluates to a document. For example, if
61+
``employee`` and ``business`` fields are document fields,
62+
then the following :expression:`$mergeObjects` expression,
63+
which evaluates to a document, is a valid argument to
64+
:pipeline:`$sortByCounts`:
65+
66+
.. code-block:: javascript
67+
68+
{ $sortByCount: { $mergeObjects: [ "$employee", "$business" ] } }
69+
70+
However, the following example with the document literal
71+
expression is invalid:
72+
73+
.. code-block:: javascript
74+
75+
{ $sortByCount: { lname: "$employee.last", fname: "$employee.first" } }
76+
77+
.. seealso:: :doc:`/reference/bson-type-comparison-order/`
78+
5179
Behavior
5280
--------
5381

@@ -97,4 +125,5 @@ order by count:
97125
{ "_id" : "woodcut", "count" : 1 }
98126
{ "_id" : "ukiyo-e", "count" : 1 }
99127
{ "_id" : "satire", "count" : 1 }
100-
{ "_id" : "caricature", "count" : 1 }
128+
{ "_id" : "caricature", "count" : 1 }
129+

0 commit comments

Comments
 (0)