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

    • Minor Change
    • ALL
    • v4.0, v3.6, v3.4
    • 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 } } ]
    • Query 2018-10-22

    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

              ian.boros@mongodb.com Ian Boros
              jaksz Jakub Szypulka
              Votes:
              0 Vote for this issue
              Watchers:
              13 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: