Core Server
  1. Core Server
  2. SERVER-4899

aggregation: need an expression to give the size of an array in projections

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Major - P3 Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.5.3
    • Component/s: Aggregation Framework
    • Labels:
      None
    • Backport:
      No
    • Backward Breaking:
      No
    • # Replies:
      12
    • Last comment by Customer:
      true
    • Documentation changes needed?:
      Yes
    • Driver changes needed?:
      Driver changes needed

      Description

      Some analytics queries can't be done by the aggregation framework only for the lack of an array size operator:

      var a1 = db.runCommand({ aggregate:"track", pipeline:[
          {$group: {
              _id: "$urllower",
              visits: {$sum: "$visits"},
              kw: {$addToSet: "$kw"}
          },
           {$project: {
               urllower: "$_id",
               visits: 1,
               keywords: {$size: "$kw"}
           }
      ]});
      

      The request here is for the $size operator at the end of the projection, which would give the size of the keyword array being built up here.

      I choose $size for the name because we already use that for the same meaning here:
      http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24size .

        Issue Links

          Activity

          Hide
          Dan
          added a comment -

          +1 . Any reason there's no progress although this was coded a year ago? Doing $unwind can have a big performance hit for big arrays

          Show
          Dan
          added a comment - +1 . Any reason there's no progress although this was coded a year ago? Doing $unwind can have a big performance hit for big arrays
          Hide
          David Henderson
          added a comment -

          This would be very useful - just coded an alternative using $unwind and it is slow.

          Show
          David Henderson
          added a comment - This would be very useful - just coded an alternative using $unwind and it is slow.
          Hide
          Ryan Behan
          added a comment -

          +1 This would be very useful for our data.

          Show
          Ryan Behan
          added a comment - +1 This would be very useful for our data.
          Hide
          auto
          added a comment -

          Author:

          {u'username': u'dannenberg', u'name': u'matt dannenberg', u'email': u'matt.dannenberg@10gen.com'}

          Message: SERVER-4899 $size for aggregation
          Branch: master
          https://github.com/mongodb/mongo/commit/abb240549a0174f52d1ee18a8866e0ad65a10881

          Show
          auto
          added a comment - Author: {u'username': u'dannenberg', u'name': u'matt dannenberg', u'email': u'matt.dannenberg@10gen.com'} Message: SERVER-4899 $size for aggregation Branch: master https://github.com/mongodb/mongo/commit/abb240549a0174f52d1ee18a8866e0ad65a10881
          Hide
          Asya Kamsky
          added a comment -

          Note, this cannot be used to determine if the field is an array.

          Just like $unwind, this operator will give an error if applied to a non-array.

          Possibly related: SERVER-8088

          Show
          Asya Kamsky
          added a comment - Note, this cannot be used to determine if the field is an array. Just like $unwind, this operator will give an error if applied to a non-array. Possibly related: SERVER-8088

            People

            • Votes:
              21 Vote for this issue
              Watchers:
              20 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since reply:
                22 weeks, 5 days ago
                Date of 1st Reply: