[SERVER-15175] Add a way to "unwind" fields in a document. Created: 08/Sep/14  Updated: 06/Jan/17  Resolved: 12/Sep/14

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Rod Adams Assignee: Unassigned
Resolution: Duplicate Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-11392 $unwind on subdocuments Closed
Participants:
Case:

 Description   

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.



 Comments   
Comment by Ramon Fernandez Marina [ 12/Sep/14 ]

I've consolidated this ticket with SERVER-11392 and closing this ticket as a duplicate. Please tune in to SERVER-11392 for updates.

Regards,
Ramón.

Comment by Mark Hall [ 08/Sep/14 ]

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'}

Cheers,
Mark.

Generated at Thu Feb 08 03:37:11 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.