MongoDB Aggregation Framework - $unwind stage

💗 $unwind stage:

In MongoDB aggregation framework, the $unwind stage is used to deconstruct an array field from the input document and create a separate document for each element of an array.

💧 Syntax:

{ $unwind: <field path > }

When using this syntax, 

$unwind does not output a document if the field value is null, missing, or an empty array.

or use this syntax,

{
$unwind:
{
path: <field path>,
includeArrayIndex: <string>,
preserveNullAndEmptyArrays: <boolean>
}
}


Optional.

        If true, if the path is null, missing, or an empty array, $unwind outputs the document.

        If false, if path is null, missing, or an empty array, $unwind does not output a document.

The default value is false.


$unwind stage with $group stage:

db.getCollection("persons").aggregate([

{$unwind: "$tags"}, 

{$group: {"_id": "$tags"}}

]);



Post a Comment

Previous Post Next Post