Does $sortByCount work in compass?

I tried to create a pipeline in compass using sortByCount but I’m not getting any results.
I also saw this jira :


Let’s see your screenshots.

It was just a trial test on a local database under $sortByCount option


  • expression: Grouping expression or string.

Exporting pipeline to language returns this

A match exports returns this
$match: {
“type”: “Refinement”

This line works in mongoshell[{$sortByCount: “$type”}])

It’s a bug! Remove the curly braces and have it looking like this:
The support ticket is very disappointing, especially for a bug that was reported in version 1.16 and we’re now in version 1.20. And all it says is “Won’t fix” without any comments from the staff who closed the ticket.
@Sonali_Mamgain may have some insight :slight_smile:

That worked, thank you :slight_smile:

You also have to remove any extra blank lines, the expression has to be right after the comments.

( Might be a problem in the token parsing yeah, or maybe the syntax analysis. )

Wow : When are they going to fix it ?

Hi folks,

COMPASS-3195 was closed as “Won’t fix” as this is expected behaviour supporting $sortByCount’s syntax where either a grouping expression or a string can be provided.

The $sortByCount template in Compass’ Aggregate Pipeline Builder currently defaults to the grouping expression which is listed first: the placeholder is expression rather than string.

Unfortunately the closing comment from @Massimiliano_Marcon on COMPASS-3195 was accidentally hidden:

you don’t need to remove the comments, just the curly brackets. I understand this might be a bit annoying but it is not a bug. The template for $sortByCount has curly brackets because it’s possible to specify an expression that evaluates to a document (

In your case, for example, a $sortByCount with this expression would lead to the same result:

{$mergeObjects: [‘$airplane’]}

This could be more clearly indicated in the Aggregation Pipeline Builder to avoid confusion, as a simple field path (which would be the string variation for a $sortByCount stage) is also commonly used. I created a Jira issue: COMPASS-4996: Improve $sortByCount template example to clarify expression vs string syntax.


1 Like

Hello @Stennie_X
that was a fast response! To reduce confusion, could it be helpful to extended the comment for the “string” in the aggregation builder? Both options are already mentioned there, the template defaults to the “expression”.