Updated March 6, 2023
Definition of Mongodb unwind
MongoDB unwind operator is used to deconstructing the array field from input to output documents, it will be used for each element from the document. The difference between input and output document in unwind operator is very simple, the output document value of a field of array is replaced by a single item of the input array of documents. MongoDB unwind operator is basically used for transfer complex documents into simple documents, it will improve the documents readability and understanding. Using unwind operator in MongoDB we can also perform operations like grouping and sorting on the data.
Syntax:
Below is the syntax of unwind operator in MongoDB.
1) {$unwind: <Path of field>}
2)
{
$unwind:
{
Path: <Path of field>,
IncludeArrayIndex : <Type is string>,
PreserveNullEmptyArrays : <Type is Boolean>
}
}
Parameter description syntax of unwind operator in MongoDB
1) Unwind operator –
This operator is used to deconstruct the documents in MongoDB. Every output and input documents depend on each other to deconstruct the value. We have passing the input parameter with unwind operator to display the result. Unwind operator is used with prefix as $ while using in MongoDB.
2) Path –
Type of this parameter in MongoDB unwind operator is a string. This is the path field of an array, path is used to specify the path of documents. This is a mandatory parameter while using unwind operator in MongoDB.
3) IncludeArrayIndex –
This is an optional parameter while using unwind operator in MongoDB. Type of this parameter in MongoDB unwind operator is a string. This states that the new name of a field is used to hold the index array for the element. This parameter name does not start with the $ sign.
4) PreserveNullEmptyArrays –
Type of this parameter in MongoDB unwind operator is Boolean. PreserveNullEmptyArrays is an optional parameter while using unwind operator in MongoDB. If the path of this parameter contains the true value unwind operator will show the output, if the path of this parameter contains the false value then unwind operator will not show the output.
How unwind work in Mongodb?
MongoDB unwind operator will deconstructs the documents for every document. Unwind operator is basically works on array elements. We can also use embedded documents with unwind operators.
I suppose our array contains the ABC student mark as {50, 55, 60, 70, 75}. Unwind operator will return the output as below.
{Name: “ABC”, mark: 50}
{Name: “ABC”, mark: 55}
{Name: “ABC”, mark: 60}
{Name: “ABC”, mark: 70}
{Name: “ABC”, mark: 75}
The above output shows that the array will be deconstructs into multiple documents. Our array contains the single documents value, but we can see the output will show the multiple documents in it.
We can pass includeArrayIndex and preserveNullEmptyArrays parameter while using unwind operator in MongoDB. Both parameters are optional while using unwind operator.
Unwind operator will duplicates each array element into different documents. This is used in an array that contains the data like a month, day of the week, and year.
Unwind operator is also working with the non-array path field. Before MongoDB version 3.2 if we have used a non-array path field it will show an error. After version 3.4 every non-array path field will not show any error it will return single elements of an array.
In the below example, we have used the non-array path field as a name. After using the non-array path field the array element will retrieve the single document.
Code:
db.MongoDB_Update.aggregate ([{$unwind: "$name"}])
db.MongoDB_Update.find ()
Figure – Unwind operator is work with non-array path field in MongoDB.
If we have missed any value in the path, unwind operator will not generate any output if we have entered an incorrect value.
Code:
db.MongoDB_Update.aggregate ([{$unwind: "$id"}])
db.MongoDB_Update.find ()
Figure – unwind operator will not generate any output if we have entered any incorrect value.
In the above example, we have used id field in unwind operator, but id field is not present in MongoDB_Update collection. So unwind operator will return the empty result in output.
Example
The below example shows unwind operator in MongoDB.
1) Unwind operator with array field –
In the below example, we have used the array field name as lap_storage. After using the array field we can see that result will display each document with a different field.
Lap_storage contains the 6 array elements and MongoDB_Update collection contains the 2 documents, so we can see that unwind operator displays output as 12 documents.
db.MongoDB_Update.aggregate ([{$unwind: "$lap_storage"}])
db.MongoDB_Update.find ()
Figure – Example of unwind operator with array field.
2) Unwind operator with includeArrayIndex parameter –
In the below example, we have used includeArrayIndex parameter with unwind operator. We have used array field name as lap_storage and includeArrayIndex field as MongoDBIndex.
MongoIndex is a user-defined field that was used to capture the array index from lap_storage field.
Code:
db.MongoDB_Update.find ()
db.MongoDB_Update.aggregate ([{$unwind: {path: "$lap_storage", includeArrayIndex: "MongoDBIndex"}}])
Figure – Example of Unwind operator with includeArrayIndex parameter.
3) Unwind operator with preserveNullEmptyArrays parameter with true value –
In the below example, we have used preserveNullEmptyArrays parameter with unwind operator. We have used array field name as lap_storage and preserveNullEmptyArrays parameter value as true.
Code:
db.MongoDB_Update.aggregate ([{$unwind: {path: "$lap_storage", preserveNullAndEmptyArrays: true}}])
db.MongoDB_Update.find ()
Figure – Example of Unwind operator with preserveNullEmptyArrays parameter with true value
4) Unwind operator with preserveNullEmptyArrays parameter with false value –
In the below example, we have used preserveNullEmptyArrays parameter with unwind operator. We have used array field name as lap_storage and preserveNullEmptyArrays parameter value as false.
Code:
db.MongoDB_Update.aggregate ([{$unwind: {path: "$lap_storage", preserveNullAndEmptyArrays: false}}])
db.MongoDB_Update.find ()
Figure – Example of Unwind operator with preserveNullEmptyArrays parameter with false value.
5) Unwind operator using embedded documents –
The below example shows that unwind operator using embedded documents. We have embedded document field name as lap_spec.
Also, we have used preserveNullEmptyArrays parameter with unwind operator. We have set the value of preserveNullEmptyArrays parameter as true.
Code:
db.MongoDB_Update.aggregate ([{$unwind: {path: "$lap_spec", preserveNullAndEmptyArrays: true}}]).pretty ()
db.MongoDB_Update.find ()
Figure – Example of unwind operator using embedded documents in MongoDB.
Conclusion
Unwind operator is very useful and important in MongoDB to deconstruct the array field. We have using preserveNullEmptyArrays and includeArrayIndex optional parameter while using unwind operator. We can also use embedded documents with unwind operators. We can transfer complex documents into simple documents by using unwind operator in MongoDB.
Recommended Articles
This is a guide to Mongodb unwind. Here we discuss the definition, syntax, How unwind works in Mongodb? Examples, and code implementation. You may also have a look at the following articles to learn more –