In this lecture, I want to talk about the dollar unwind stage.
This stage deconstructs an array field from the input documents to output a document for each element.
Each output document is the input document with the value of the array field replaced by the element.
The syntax is as follows.
The path field is the field that contains the array that you would like to deconstruct.
The include array index field and the preserve null and empty arrays.
Fields are optional.
Include array index returns, the index number for each element inside the array of the input document.
For the value of this field.
You pass in a string that you would like to name this field in the output document.
Preserve null and empty arrays takes a boolean of true or false.
When true, it returns documents where the field that is passed into the dollar unwind stage has null
or empty values.
But by default, this is false.
So let's jump into some demonstrations.
So this all makes a bit more sense.
As you can see, I'm in the shell connected to my Atlas cluster and on the sample underscore training
database.
Let's query one document from the grades field.
Okay.
So the scores field is an array.
Each element is a document.
If we wanted to see a document for each of these individual elements, we can use the dollar unwind
stage.
So we can type db dot grades dot aggregate.
Dollar unwind.
And for the path we specify the dollar scores field.
And as you can see.
The school field now contains one element.
Notice how the ID fields.
In these four documents are the same.
This is because previously these four documents were a single document where the scores field contained
an array of each of these elements.
In the field.
Okay.
So currently we have the default settings for include array index and preserve null and empty arrays.
So let's set include array index to a field value.
So if I return my previous query.
I can add.
Include.
Array Index.
I give this a name of array underscore ID.
And hit enter.
Now, as you can see, we have an array index.
So notice the object ID is the same for these four documents.
This has an index of zero one, two and three, and that's because the array index starts from zero.
Okay.
So preserve null and empty arrays by default is set to false.
This means that if the array is empty in the input document.
Then it's not returned.
So if I set it to false now.
By adding it on.
Then there'll be no change.
As you can see.
But now if I unwind to another field that doesn't exist.
Such as?
Let's call this.
Scores two and scores two doesn't exist in the grades document.
We return no documents.
Because null and empty arrays are not returned by setting preserved null and empty arrays as false.
If I now set it to true.
Then we return all documents.
But the documents are unchanged from the inputs.
You'll notice that the scores field now still contains an array of the four documents.
That's because there is no schools to field, so nothing was unwound.
However, because we set preserve null and empty arrays as false, the scores to field, although it
technically doesn't exist because null and empty arrays are still returned, each document is returned,
but it's essentially unchanged from the input document.
One final note is that if you wanted to unwind documents and you didn't need to use the include array
index or the preserve null array fields, there's a simpler syntax you can use.
You can just type DB upgrades.
Dot aggregate.
Passing.
Passing the unwind stage.
And then just specify your field.
You don't need to include the path field.
And this also works.
Great.
For more information in the unwind stage, please refer to the official documentation on MongoDB.
You can follow this URL or simply just type dollar unwind.
MongoDB into Google.