Javatpoint Logo
Javatpoint Logo

MongoDB $unwind operator

MongoDB provides a variety of state operators. The $unwind operator is one of those operators. The $unwind operator is used to deconstructing an array field in a document and create separate output documents for each item in the array.

The only difference between input and output documents is that the value of the array field in output documents is replaced with a single item from the input document array. The $unwind operator breaks complex documents into smaller pieces, making them easier to read and understand.

Syntax:

Important points:

  1. If the value of the field-path option is not an array, it generates an error.
  2. If the field path does not exist in the input document, it displays no output.
  3. If the array is empty, it displays no output.

Let us take an example to better understand the concept of the $unwind operator.

Example 1: Using $unwind operator on the array

Create an employee collection.

Now, display the documents from employee collection using the find() method.

Output:

{
    "_id" : ObjectId("456187864hfh5421h510"),
    "name" : "Mikky",
    "age" : 31,
    "phone_no" : 8654793212
    "company" : "javatpoint",
    "skills" : [ 
        "C", 
        "C++",
        "PHP", 
        "Java", 
        ".Net"
    ]
}

As you can see the "skills" field is an array containing 5 items ("C", "C++", "PHP", "Java", ".Net").

Now, use the $unwind operator and see how the output looks like.

Output:

/* 1 */
{
    "_id" : ObjectId("456187864hfh5421h510"),
    "name" : "Mikky",
     "phone_no" : 8654793212,
    "age" : 31,
    "skills" : "C"
}

/* 2 */
{
    "_id" : ObjectId("456187864hfh5421h510"),
    "name" : "Mikky",
     "phone_no" : 8654793212,
    "age" : 31,
    "skills" : "C++"
}

/* 3 */
{
    "_id" : ObjectId("456187864hfh5421h510"),
    "name" : "Mikky",
     "phone_no" : 8654793212,
    "age" : 31,
    "skills" : "PHP"
}

/* 4 */
{
    "_id" : ObjectId("456187864hfh5421h510"),
    "name" : "Mikky",
     "phone_no" : 8654793212,
    "age" : 31,
    "skills" : "Java"
}

/* 5 */
{
    "_id" : ObjectId("456187864hfh5421h510"),
    "name" : "Mikky",
     "phone_no" : 8654793212,
    "age" : 31,
    "skills" : ".Net"
}

As you can see in the output, we have all five skills in separate elements.

Example 2: Using $unwind operator on embedded arrays

When you use the $unwind operator on an embedded array, it acts the same way as a regular array.

Now, create a product collection with the following documents.

Now, the $unwind operator is performed on the "items" embedded arrays in the above document.

Output:

{ 
     "_id" : "1", 
     "items" : { 
                 "name" : "copy", 
                 "work" : [ "write", "office" ], 
                 "cost" : 10, 
                 "total_quantity" : 5 
               } 
}
{  a
     "_id" : "1", 
     "items" : { 
                 "name" : "pencil", 
                 "work" : [ "write", "school" ], 
                 "cost" : 2, 
                 "total_quantity" : 5 
               } 
}
{     
      "_id" : "2", 
      "items" : { 
                  "name" : "monitor", 
                  "work" : [ "collage", "office" ], 
                  "cost" : 5000, 
                  "total_quantity" : 1 
                } 
}
{ 
      "_id" : "2", 
      "items" : { 
                  "name" : "mouse", 
                  "work" : [ "laptop", "CPU" ], 
                  "cost" : 300, 
                  "total_quantity" : 5 
                } 
}

As you can see in the output, we have all the four items of the embedded arrays separated. You can further break it down using the "work" array.

Output:

{ "_id" : "1", "items" : { "name" : "copy", "work" : "write", "cost" : 10, "total_quantity" : 5 } }
{ "_id" : "1", "items" : { "name" : "copy", "work" : "office", "cost" : 10, "total_quantity" : 5 } }
{ "_id" : "1", "items" : { "name" : "pencil", "work" : "write", "cost" : 2, "total_quantity" : 5 } }
{ "_id" : "1", "items" : { "name" : "pencil", "work" : "school", "cost" : 2, "total_quantity" : 5 } }
{ "_id" : "2", "items" : { "name" : "monitor", "work" : "collage", "cost" : 5000, "total_quantity" : 1 } }
{ "_id" : "2", "items" : { "name" : "monitor", "work" : "office", "cost" : 5000, "total_quantity" : 1 } }
{ "_id" : "2", "items" : { "name" : "mouse", "work" : "laptop", "cost" : 300, "total_quantity" : 5 } }
{ "_id" : "2", "items" : { "name" : "mouse", "work" : "CPU", "cost" : 300, "total_quantity" : 5 } }






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA