Aggregation task - $unwind

A field producing task that returns a duplicated document for each value in the specified array.

The $unwind task produces a duplicated document for each value in an array, such that in the resulting document the array field object is replaced by that value.

Syntax diagram

Read syntax diagramSkip visual syntax diagram$unwind:fieldname

Command arguments

fieldname
The name of an array field. The name must be preceded with $.

Example

Consider a set of documents that contain a name, year (array), and city:

john, [2002], washington
jane, [2002, 2005], philadelphia
db.article.aggregate(
 { $project : { name : 1, year : 1, city: 1 } },
 { $unwind : "$year" }
) 

Sample output:

john, 2002, washington
jane, 2002, philadelphia
jane, 2005, philadelphia