MongoDB.live, free & fully virtual. June 9th - 10th. Register Now MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Compass, Aggregation & $unwind

when setting up an aggregation pipeline stage the $unwind selection defaults to long mode… so for the short mode on a simpler single array field are we to manually back all that out and just enter a field?..attempts at this approach don’t seem to work…or is $unwind: not to be set up in its own stage?..

Suppose if your collection has document like this: { _id: 1, arr: [ "app", "ban", "ora" ] }. The Compass’s aggregation $unwind pipeline stage for the field arr would be:

{
    path: "$arr"
}

Edit Add:
The default display will have the following content - edit it to your needs (e.g., as above):

/**
 * path - Path to the array field.
 * includeArrayIndex - Optional name for index.
 * preserveNullAndEmptyArrays - Optional
 *   toggle to unwind null and empty values.
 */
{
  path: path,
  includeArrayIndex: 'string',
  preserveNullAndEmptyArrays: boolean
}

Hi @James_Bailie,

What do you mean by “long mode” and “short mode”? The $unwind stage takes an array and creates a document for every element in the array (i.e. start with n documents, and with m documents where m >= n).

What are you trying to accomplish? I want to better understand your question before attempting to answer.

I think the short and long “modes” are the two syntax versions $unwind can be used with: Field Path Operand and Document Operand with Options.

1 Like

so to answer Justin: that is the terminology of the lesson/video course M121 on the aggregation pipeline. we are taught the short mode for command line i.e.:
{$unwind: “$genres”},

at the end of the video the long mode is noted/introduced but only that FYI it exists - and nothing further (the 3 parameters per Prasad’s reply)

I am experimenting with the course assignments in doing them both as shell commands, and then over in Compass. The class is pretty much all shell commands.

Over in Compass, in the Aggregation tab, when you select stage $unwind - - it presents the long mode…so that was a head scratcher… but Prasad has answered it in providing the syntax to be:
path: “$arr” i.e. path: <$field name>
…which maybe was so easy, in retrospect, that it never occurred to me…

Got it. To clarify terminology, there isn’t really a “long mode” and “short mode”. MongoDB University likely wanted to call out that a different exists and needed a term to draw attention to the difference.

The distinction here is about what type of operand is provided to the $unwind stage. Check out the documentation page for details:
https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/

Basically, there is a string operand or document operand. Both do exactly the same thing but may subtly change the output. For example, it can be helpful to keep track of where in the array an element existed after $unwind by using includeArrayIndex: true.

I recommend always reading the documentation when working with aggregation stages. Simply reading through the different options often clarifies a lot.

I hope that helps!

Justin