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

$project computed fields on arrays sets every element of the array -- inconsistent with $set

    • Query
    • ALL
    • Hide
      > db.test.insert({_id: 0, a: [1, 3, 5]})
      > db.test.aggregate({$project: {"a.0": {$literal: 10}}})
      
      Show
      > db.test.insert({_id: 0, a: [1, 3, 5]}) > db.test.aggregate({$project: {"a.0": {$literal: 10}}})

      > db.test.insert({_id: 0, a: [1, 3, 5]})
      > db.test.aggregate({$project: {"a.0": {$literal: 10}}})
      { "_id" : 0, "a" : [ { "0" : 10 }, { "0" : 10 }, { "0" : 10 } ] }
      

      This behavior replaces every element of the array, when what $set does is more elegant:

      > db.test.update({a: [1, 3, 5]}, {$set: {"a.0": 10}})
      > db.test.find()
      { "_id" : 0, "a" : [ 10, 3, 5 ] }
      

      However, $set errors when "a" is not an array.

      This behavior occurs in both the $project stage and the new $addFields stage.

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            carly.robison Carly Robison
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: