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

$unwind of non-array should be allowed

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.1.0
    • Component/s: Aggregation Framework
    • Labels:
      None
    • Backwards Compatibility:
      Minor Change
    • Operating System:
      ALL

      Description

      This would effectively just output the existing field.

      > db.reg.find()
      { "_id" : 1, "text" : "foo" }
      { "_id" : 2, "text" : "bar" }
      { "_id" : 3, "text" : "Bar" }
      { "_id" : 4, "text" : [ "bar", "foo" ] }
      >db.reg.aggregate({$match:{text:/ba/i}}, {$unwind:"$text"})
      ...
      	"errmsg" : "exception: $unwind:  value at end of field path must be an array",
      	"code" : 15978,
      	"ok" : 0

      This will be important for collections where some document fields may be arrays, but some may not.

        Issue Links

          Activity

          Hide
          sfrenkiel Scott Frenkiel added a comment -

          Small world..I enjoyed the talk also. Had never heard of mtools, but it's been helpful for us

          We'll look into this workaround.

          Show
          sfrenkiel Scott Frenkiel added a comment - Small world..I enjoyed the talk also. Had never heard of mtools, but it's been helpful for us We'll look into this workaround.
          Hide
          asya Asya Kamsky added a comment -

          Laurent Dollé

          One possible workaround would be to catch the exception when a non-array is attempted to be unwound and then rewrite the query programmatically based on the error.

          I do want to point out that aggregation is not the only part of the system that can error when array vs. non-arrays are inconsistent through the collection - any array operation like "$push", etc. will also give an error if the targeted field is not of the expected type, so this is not something introduced by aggregation framework.

          Possible upstream workaround would involve normalizing all fields which are allowed to be arrays to always be arrays only - but since I don't know where the data comes from I cannot say whether that's feasible.

          Asya

          Show
          asya Asya Kamsky added a comment - Laurent Dollé One possible workaround would be to catch the exception when a non-array is attempted to be unwound and then rewrite the query programmatically based on the error. I do want to point out that aggregation is not the only part of the system that can error when array vs. non-arrays are inconsistent through the collection - any array operation like "$push", etc. will also give an error if the targeted field is not of the expected type, so this is not something introduced by aggregation framework. Possible upstream workaround would involve normalizing all fields which are allowed to be arrays to always be arrays only - but since I don't know where the data comes from I cannot say whether that's feasible. Asya
          Hide
          pasette Dan Pasette added a comment -

          null field, empty array, missing field should all currently emit no document, which is correct.
          We will change agg to emit a single document containing the scalar value as requested.

          Show
          pasette Dan Pasette added a comment - null field, empty array, missing field should all currently emit no document, which is correct. We will change agg to emit a single document containing the scalar value as requested.
          Hide
          ldolle@amadeus.com Laurent Dollé added a comment -

          Dan Pasette,
          Are the "other parts of the system that can error" (e.g., $push - cf. Asya's comment) also impacted by this new behaviour or are we only dealing here with unwind?
          Thanks anyway for increasing the severity and considering a quick fix.

          – Laurent

          Show
          ldolle@amadeus.com Laurent Dollé added a comment - Dan Pasette , Are the "other parts of the system that can error" (e.g., $push - cf. Asya's comment) also impacted by this new behaviour or are we only dealing here with unwind? Thanks anyway for increasing the severity and considering a quick fix. – Laurent
          Hide
          xgen-internal-githook Githook User added a comment -

          Author:

          {u'username': u'erh', u'name': u'Eliot Horowitz', u'email': u'eliot@10gen.com'}

          Message: SERVER-8088: $unwind of scalar should return 1 doc with scalar
          Branch: master
          https://github.com/mongodb/mongo/commit/f4d17dd81431f9724006c0837ccac44068971b1d

          Show
          xgen-internal-githook Githook User added a comment - Author: {u'username': u'erh', u'name': u'Eliot Horowitz', u'email': u'eliot@10gen.com'} Message: SERVER-8088 : $unwind of scalar should return 1 doc with scalar Branch: master https://github.com/mongodb/mongo/commit/f4d17dd81431f9724006c0837ccac44068971b1d

            People

            • Votes:
              11 Vote for this issue
              Watchers:
              18 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: