[SERVER-6365] New aggregation pipeline operator: $count Created: 09/Jul/12  Updated: 06/Dec/22  Resolved: 03/Feb/17

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

Type: Improvement Priority: Major - P3
Reporter: Nathan Wells Assignee: Backlog - Query Team (Inactive)
Resolution: Done Votes: 6
Labels: accumulator, expression, stage
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to SERVER-12927 Support ROLLUP function in the $group... Backlog
is related to SERVER-23836 add $count stage to aggregation Closed
Assigned Teams:
Query
Participants:

 Description   

here's a list of documents:

{_id:1, manufacturer: "Sony", features: ["fast", "sleek", "sexy"]}
{_id:2, manufacturer: "Sony", features: ["lightweight", "sleek"]}
{_id:3, manufacturer: "Sony", features: ["cheap", "fast"]}
{_id:4, manufacturer: "Apple", features: ["fast", "sleek", "usable"]}
{_id:5, manufacturer: "Apple", features: ["lightweight", "sleek", "cheap"]}
{_id:6, manufacturer: "Apple", features: ["fast", "usable", "codecs"]}

I'd like to output to look like:

{
  "manufacturer": {
    "Sony":3,
    "Apple":3
  },
  "features": {
    "fast": 4,
    "cheap": 2,
    "codecs": 1,
    "usable": 2,
    "lightweight": 2,
    "sleek": 3,
    "sexy": 1,
  }
}

Here's my opinion on how to make this request:

db.coll.aggregate(
  {$count: ["manufacturer", "features"]}
)

Alternately, I'd be fine with a new group aggregation function, like so:

db.coll.aggregate({
  $group: {
    manufacturer: {$count: "$manufacturer"},
    features: {$count: "$features"}
  }
})

Although this obviously requires that the need to specify an _id be lifted, or accounted for in some way.



 Comments   
Comment by Charlie Swanson [ 03/Feb/17 ]

Hi nwwells,

I'm closing this ticket since we have since introduced a number of new operators in the aggregation framework which I believe could achieve your requested functionality. In particular, we have added a $count stage, a $sortByCount stage, and a $facet stage.

Comment by Asya Kamsky [ 03/Jan/17 ]

This ticket needs clarification - $count already exists and does something different. There is also $facet which can do something similar to this, and there's request for rollup expression which is similar.

Generated at Thu Feb 08 03:11:28 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.