Core Server
  1. Core Server
  2. SERVER-4589

aggregation: need an array indexing operator

    Details

    • Backport:
      No
    • # Replies:
      5
    • Last comment by Customer:
      true

      Description

      At present, there's no way to extract a specific element from an array-valued field. An operator should be added that could be used in a projection, e.g.:

      db.runCommand({aggregate:"c", pipeline:[
      { $project : {
      foo :

      { $at:["$arrayField", 5] }

      }}
      ]});

      The effect of this would be put add the computed field (virtual field) foo to the projection, and give it the value of arrayField[5].

        Issue Links

          Activity

          Hide
          Scott Hernandez
          added a comment -

          Should this support something like $slice as well?

          Show
          Scott Hernandez
          added a comment - Should this support something like $slice as well?
          Hide
          Chris Westin
          added a comment -

          $slice ounds like a good idea. Going after a single element is a special case of $slice, so it seems like we should just implement that, and support functional use ($slice) as well as array element syntax to call the same thing.

          Show
          Chris Westin
          added a comment - $slice ounds like a good idea. Going after a single element is a special case of $slice, so it seems like we should just implement that, and support functional use ($slice) as well as array element syntax to call the same thing.
          Hide
          Gavin Hogan
          added a comment -

          This would be very helpful in combination with $unwind.

          Show
          Gavin Hogan
          added a comment - This would be very helpful in combination with $unwind.
          Hide
          Christian Csar
          added a comment -

          I think it's important to note that for $match this is not a new feature, rather it appears to be a bug in a currently existing feature (and arguably for $project as well). See the documented syntax at http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29#DotNotation%28ReachingintoObjects%29-ArrayElementbyPosition and the language for $match which says "The syntax passed to the $match is identical to the query syntax." See https://groups.google.com/d/topic/mongodb-user/7o5frOLbPQ8/discussion for examples showing limitations of $match vs query in regard to this.

          At present this makes it seemingly impossible to make use of the individual values in geospatial fields. I'd appreciate it if this was reclassified as a bug.

          Show
          Christian Csar
          added a comment - I think it's important to note that for $match this is not a new feature, rather it appears to be a bug in a currently existing feature (and arguably for $project as well). See the documented syntax at http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29#DotNotation%28ReachingintoObjects%29-ArrayElementbyPosition and the language for $match which says "The syntax passed to the $match is identical to the query syntax." See https://groups.google.com/d/topic/mongodb-user/7o5frOLbPQ8/discussion for examples showing limitations of $match vs query in regard to this. At present this makes it seemingly impossible to make use of the individual values in geospatial fields. I'd appreciate it if this was reclassified as a bug.
          Hide
          Asya Kamsky
          added a comment -

          Note: $match does allow referencing positions of array, at least in the latest master:

          > db.array.aggregate({$match:{"a.0":1}})
          { "_id" : ObjectId("5282869acefcd1a08068e472"), "a" : [  1,  2,  3 ] }
          { "_id" : ObjectId("5282869fcefcd1a08068e473"), "a" : [  1 ] }
          Fetched 2 record(s) in 0ms
          

          This allows matching only documents where a is array or a is non-array, but the same doesn't work in {{ $project {$cond} }} for example.

          Show
          Asya Kamsky
          added a comment - Note: $match does allow referencing positions of array, at least in the latest master: > db.array.aggregate({$match:{"a.0":1}}) { "_id" : ObjectId("5282869acefcd1a08068e472"), "a" : [ 1, 2, 3 ] } { "_id" : ObjectId("5282869fcefcd1a08068e473"), "a" : [ 1 ] } Fetched 2 record(s) in 0ms This allows matching only documents where a is array or a is non-array, but the same doesn't work in {{ $project {$cond} }} for example.

            People

            • Votes:
              46 Vote for this issue
              Watchers:
              28 Start watching this issue

              Dates

              • Created:
                Updated:
                Days since reply:
                21 weeks, 6 days ago
                Date of 1st Reply: