EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials MongoDB Tutorial Mongodb unwind

Mongodb unwind

Updated March 6, 2023

Mongodb unwind

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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 ()

unwind work in Mongodb 1

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 ()

unwind work in Mongodb 2

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 ()

3

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"}}])

4

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 ()

5

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 ()

Unwind operator with preserveNullEmptyArrays parameter with false value

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 ()

Unwind operator using embedded documents

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 –

  1. MongoDB Import
  2. MongoDB findOne()
  3. MongoDB group by
  4. MongoDB Join
C++ PROGRAMMING Course Bundle - 9 Courses in 1 | 5 Mock Tests
37+ Hour of HD Videos
9 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ASP.NET Course Bundle - 28 Courses in 1 | 5 Mock Tests
123+ Hours of HD Videos
28 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SQL Course Bundle - 51 Courses in 1 | 6 Mock Tests
204+ Hours of HD Videos
51 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SOFTWARE TESTING Course Bundle - 13 Courses in 1
53+ Hour of HD Videos
13 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MONGODB Course Bundle - 6 Courses in 1
 20+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more