-
Type: New Feature
-
Resolution: Duplicate
-
Priority: Major - P3
-
None
-
Affects Version/s: 2.4.7
-
Component/s: Aggregation Framework
-
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'}
- duplicates
-
SERVER-18794 Add an aggregation expression to convert an object to an array of key, value pairs
- Closed
- is duplicated by
-
SERVER-15175 Add a way to "unwind" fields in a document.
- Closed
-
SERVER-17709 New aggregation operator - $unravel
- Closed
- related to
-
SERVER-5947 Add ability to project key names as values and values as key names
- Closed