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

$unwind on subdocuments

    • Query

      $unwind, or a new operator, should emit new records for each key-value pair in a subdocument. A good example of where this would be useful is if a document contained a histogram (e.g. counts of unique account ids), and one wanted to use the aggregation framework to calculate the number of unique keys in that histogram (e.g. number of unique accounts, i.e. the length of the subdocument).

      Additional description from SERVER-15175:

      Example function:

      db.coll.insert({id: 'foo', a: 100, b: 200, c: 300, d: {e: 400, f:500}})
      db.coll.aggregate([{$docUnwind: {id: 0}},  // syntax similar to $project for whitelisting or blacklisting which fields to unwind
      ])
      {id: 'foo', f_name: 'a', f_value: 100}
      {id: 'foo', f_name: 'b', f_value: 200}
      {id: 'foo', f_name: 'c', f_value: 300}
      {id: 'foo', f_name: 'd.e', f_value: 400}
      {id: 'foo', f_name: 'd.f', f_value: 500}
      

      In short, it will take the fields in a document, and unwind a portion of them into name, value pairs, making a separate document for each.

      One use case for this is collecting statistics across a dynamic list of fields.

      And a request, also from SERVER-15175:

      It is critical (for our use case) to have the type of the field (BSON id) in the output as well - e.g.

      {id: 'foo', f_name:  'a', f_type: 1, f_value: 100}
      

      Some examples for arrays:

      db.coll.insert({id: 'foo', a: [100, 200], b : [{c: 300, f: 'fred'}]})
      db.coll.aggregate([{$docUnwind: {id: null}}, ...])  // process all documents like $group can
      {id: 'foo', f_name: 'a[]', f_type: 1} // the [] in the name tells us that this is a terminal array of primitives; value omitted in this case perhaps?
      {id: 'foo', f_name: 'b[].c', f_type: 1, f_value: 300}
      {id: 'foo', f_name: 'b[].f',  f_type: 2, f_value: 'fred'}
      

            Assignee:
            backlog-server-query Backlog - Query Team (Inactive)
            Reporter:
            aaron.westendorf Aaron Westendorf
            Votes:
            9 Vote for this issue
            Watchers:
            15 Start watching this issue

              Created:
              Updated:
              Resolved: