[SERVER-23816] Add $sortByCount aggregation stage Created: 19/Apr/16  Updated: 14/Mar/17  Resolved: 16/Jun/16

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: 3.3.9

Type: New Feature Priority: Major - P3
Reporter: Charlie Swanson Assignee: Sally McNichols
Resolution: Done Votes: 0
Labels: stage
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by CSHARP-1693 Add $sortByCount aggregation stage Closed
is depended on by JAVA-2232 Add builder for $sortByCount aggregat... Closed
Documented
is documented by DOCS-8831 Faceted Search - $sortByCount Closed
Related
related to DRIVERS-297 Aggregation Framework Support for 3.4 Closed
is related to SERVER-1635 Faceting support Closed
Backwards Compatibility: Fully Compatible
Sprint: Query 16 (06/24/16)
Participants:

 Description   

Syntax

{ $sortByCount: <arbitrary non-object expression, see 'Behavior' for why> }

Examples

> db.example.insert([
  {_id: 0, x: 1},
  {_id: 1, x: 2},
  {_id: 2, x: 1},
  {_id: 3, x: 0}
]);
> db.example.aggregate([{$sortByCount: "$x"}])
{_id: 1, count: 2}
{_id: 0, count: 1}
{_id: 2, count: 1}
 
// Example #2
> db.example.drop();
> db.example.insert([
  {_id: 0, x: 1.4},
  {_id: 1, x: 2.3},
  {_id: 2, x: 1.1},
  {_id: 3, x: 0.5}
]);
// Note this is an expression, so does not count as an "object" for the sake of erroring.
> db.example.aggregate([{$sortByCount: {$floor: "$x"}}])
{_id: 1, count: 2}
{_id: 0, count: 1}
{_id: 2, count: 1}
 
// Example #3
> db.example.aggregate([{$sortByCount: {field1: "$x", field2: "$y"}}])
Error!

Behavior

  • This stage is syntactic sugar for the following:

    {
      $group: {
        _id: <expression>,
        count: {$sum: 1}
      }
    },
    {$sort: {count: -1}}
    

  • We restrict the expression to not be an object so that we might be able to add further arguments to $sortByCount in the future.


 Comments   
Comment by Githook User [ 16/Jun/16 ]

Author:

{u'username': u'smcnichols', u'name': u'Sally McNichols', u'email': u'sally.mcnichols@mongodb.com'}

Message: SERVER-23816 Add $sortByCount aggregation stage
Branch: master
https://github.com/mongodb/mongo/commit/069be92b4f2c35140a711cb47d88baf961d74795

Generated at Thu Feb 08 04:04:33 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.