Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-6365

New aggregation pipeline operator: $count

    XMLWordPrintable

Details

    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.

      Attachments

        Issue Links

          Activity

            People

              backlog-server-query Backlog - Query Team (Inactive)
              nwwells Nathan Wells
              Votes:
              6 Vote for this issue
              Watchers:
              12 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: