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

Different values when referencing whole object vs. a field of that object after $arrayToObject

    XMLWordPrintable

    Details

    • Backwards Compatibility:
      Minor Change
    • Operating System:
      ALL
    • Backport Requested:
      v4.0, v3.6, v3.4
    • Steps To Reproduce:
      Hide

      Collection

      [
        {
          "array": [
            {
              "k": "field",
              "v": 1
            },
            {
              "k": "field",
              "v": 2
            }
          ]
        }
      ]
      

      Query

      db.collection.aggregate([
        {
          $project: {
            object: {
              $arrayToObject: "$array"
            }
          }
        },
        {
          $project: {
            "object": "$object",
            "field": "$object.field"
          }
        }
      ])
      

      Expected output

      [
        {
          "field": 1,
          "object": {
            "field": 1
          }
        }
      ]
      

      Actual output

      [
        {
          "field": 1,
          "object": {
            "field": 2
          }
        }
      ]
      

      Show
      Collection [ { "array": [ { "k": "field", "v": 1 }, { "k": "field", "v": 2 } ] } ] Query db.collection.aggregate([ { $project: { object: { $arrayToObject: "$array" } } }, { $project: { "object": "$object", "field": "$object.field" } } ]) Expected output [ { "field": 1, "object": { "field": 1 } } ] Actual output [ { "field": 1, "object": { "field": 2 } } ]
    • Sprint:
      Query 2018-10-22
    • Case:

      Description

      Using $arrayToObject on an array of {k: ..., v: ...} objects where some k are repeating outputs an object that behaves inconsistently.

      Referencing this object as a whole using "$object" outputs an object with a field value that is different compared to the value obtained by referencing the field directly using "$object.field".

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                14 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: