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

new field name changes expression result for array in $project

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • Labels:
      None
    • Fully Compatible
    • ALL

      Or another way of stating the issue is, I cannot change array field to non-array field if I want to use any of its contents in the expression (without changing its name).

      db.coll.insert({a:[1,2]})
      

      I can change it to be embedded inside another field like this:

      db.coll.aggregate({$project:{b:{a:"$a"},_id:0}})
      { "b" : { "a" : [ 1, 2 ] } }
      

      However, if I need the top level name of new field is to be "a" then there seems to be no way to avoid projection happening for every element of original array:

      // I want to get back {"a" : {"b" : [1, 2]} }
      db.coll.aggregate({$project:{a:{b:"$a"},_id:0}})
      { "a" : [ { "b" : [ 1, 2 ] }, { "b" : [ 1, 2 ] } ] }
      

      I haven't found a way to do this except by having two $projects, one to create newA and the second to rename newA to a. I understanding adding fields to existing arrays of objects requires preserving cardinality (and type) of array field, but this seems to be a full replace.

      Looked for existing ticket related to this, SERVER-25200 is possibly the closest (though it mentions dotted notation, the behavior is similar).

            Assignee:
            asya.kamsky@mongodb.com Asya Kamsky
            Reporter:
            asya.kamsky@mongodb.com Asya Kamsky
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: