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

    • 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

      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".

        1. 屏幕快照 2018-09-19 18.01.51.png
          123 kB
          Jakub Szypulka
        2. 屏幕快照 2018-09-19 18.18.32.png
          167 kB
          Jakub Szypulka

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

              Created:
              Updated:
              Resolved: