[JAVA-2232] Add builder for $sortByCount aggregation stage Created: 21/Jun/16  Updated: 27/May/22  Resolved: 13/Sep/16

Status: Closed
Project: Java Driver
Component/s: Builders
Affects Version/s: None
Fix Version/s: 3.4.0

Type: New Feature Priority: Major - P3
Reporter: Rathi Gnanasekaran Assignee: Justin Lee
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on SERVER-23816 Add $sortByCount aggregation stage Closed
is depended on by DRIVERS-297 Aggregation Framework Support for 3.4 Closed
Epic Link: MongoDB 3.4 for Java driver
Server Compat: 3.3

 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 [ 13/Sep/16 ]

Author:

{u'username': u'evanchooly', u'name': u'Justin Lee', u'email': u'justin.lee@10gen.com'}

Message: JAVA-2232, JAVA-2255, JAVA-2256, JAVA-2275, JAVA-2277 new pipeline stages
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/582f0f35d236dd865b8dfb2ecdcdb14ef2ae3739

Comment by Githook User [ 13/Sep/16 ]

Author:

{u'username': u'evanchooly', u'name': u'Justin Lee', u'email': u'justin.lee@10gen.com'}

Message: JAVA-2232, JAVA-2255, JAVA-2256, JAVA-2275, JAVA-2277 new pipeline stages
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/eb715937752b0b0823ef6fe1c1dc4e980e8d2515

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