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

Unwind with includeArrayIndex produces different structure results for arrays and non-arrays

    • Minor Change
    • ALL
    • QuInt A (10/12/15)

      SERVER-4588 has the following behavior:

      > db.foo.find()
      { "_id" : 0, "a" : [1], "b" : "foo" }
      { "_id" : 1, "a" : 1, "b" : "bar" }
      > db.foo.aggregate({$unwind:{ path: "$a", includeArrayIndex: true}})
      { "_id" : 0, "a" : { "index" : NumberLong(0), "value" : 1 }, "b" : "foo" }
      { "_id" : 1, "a" : 1, "b" : "bar" }
      

      If I now want to do something with "a" value, I have to use an extra project with $ifNull "a.value" in order to find the value of "a" which is in "a".

      We should allow the user to specify which field the array index should go into. This will override any existing fields, and if the value was not produced from an element in an array (i.e. there is no sensical index to use), we should use the value null instead:

      > db.foo.aggregate({$unwind:{ path: "$a", includeArrayIndex: "$b"}})
      { "_id" : 0, "a" : 1, "b" : NumberLong(0) }
      { "_id" : 1, "a" : 1, "b" : null }
      

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

              Created:
              Updated:
              Resolved: