@@ -43,11 +43,39 @@ Definition
43
43
44
44
* - ``expression``
45
45
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>`,
48
50
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
+
51
79
Behavior
52
80
--------
53
81
@@ -97,4 +125,5 @@ order by count:
97
125
{ "_id" : "woodcut", "count" : 1 }
98
126
{ "_id" : "ukiyo-e", "count" : 1 }
99
127
{ "_id" : "satire", "count" : 1 }
100
- { "_id" : "caricature", "count" : 1 }
128
+ { "_id" : "caricature", "count" : 1 }
129
+
0 commit comments