$unwind Operator in MongoDB: Easy Guide

In this tutorial, I am going to explain the $unwind operator in MongoDB.

MongoDB, which includes a variety of advanced tools, is one of the most popular database management services available today. It is never easy for a developer to memorize every one of its operators and features, no matter how smart he/she may be. The fact is, it is far less common for a developer to do this than one might imagine.

An application that incorporates such a powerful database system is capable of including broad features and capabilities. In addition, enhanced security features can be implemented. I will explain how to deconstruct arrays using the $unwind operator in MongoDB in today’s article.

With MongoDB, you can choose from a wide range of operators and features. MongoDB allows you to build applications that can do much more than CRUD functions. This makes MongoDB the ideal database for building custom applications. Its popularity stems from its flexibility and power.

The database provides a variety of operators in addition to querying, printing, and changing data. Regardless of the level of experience its users possess, MongoDB still has something to offer.

Today, I am going to discuss the $unwind operator.

The $unwind operator helps in deconstructing array field values. A document for each element is derived from an array field in the input documents. Essentially, the elements of the array are replaced as part of the output document.  

This guide will walk through a few examples so you can understand how the $unwind operator works in MongoDB. So, let’s get started with the guide.

The $unwind Operator Syntax

This is what the $unwind operator syntax looks like:

{
  $unwind:
    {
      path: field path,
      includeArrayIndex: string,
      preserveNullAndEmptyArrays: boolean
    }
}

Using the $unwind Operator in MongoDB

Let us start to use the $unwind operator in different examples.

Unwinding an Array Field Using the $unwind Operator

In this example, we will use the $unwind operator to unwind an array field in MongoDB.

  • Start up the MongoDB service
  • Pick the database you want to choose and navigate into it:
show dbs
use dronesVerse
  • Taking a quick look at all the documents in the drones collection. We’ll print the output with pretty print:
> db.drones.find({}).pretty()

{
        "_id" : ObjectId("615dfce7f1ce6040485ecf62"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "RQ – Ariela Blingdom",
        "price" : 28000,
        "weight" : "3.4 kilograms",
        "additionalDetails" : {
                "material" : "polystyrene",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dff74f1ce6040485ecf63"),
        "utility" : [
                "Photography",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : true,
        "name" : "T5 – Doom Glancer",
        "price" : 8000,
        "weight" : "4 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dff94f1ce6040485ecf64"),
        "utility" : [
                "Photography",
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : true,
        "name" : "TumblE – Jumper",
        "price" : 18000,
        "weight" : "5.6 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography"
                ]
        }
}
{
        "_id" : ObjectId("615dffb0f1ce6040485ecf65"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Mount Hike Drone",
        "price" : 9000,
        "weight" : "7 kilograms",
        "additionalDetails" : {
                "material" : "aluminium",
                "moreUses" : [ ]
        }
}
{
        "_id" : ObjectId("615dffc8f1ce6040485ecf66"),
        "utility" : [
                "Videography",
                "Combat",
                "Rescue",
                "Construction"
        ],
        "onSale" : false,
        "name" : "Defender Martial X55",
        "price" : 12000,
        "weight" : "9.1 kilograms",
        "additionalDetails" : {
                "material" : "alumnium",
                "moreUses" : [
                        "Security"
                ]
        }
}
  • Let us now use the $unwind operator to deconstruct the “utility” array field:
> db.drones.aggregate( [ { $unwind: "$utility" } ] )
OR
> db.drones.aggregate( [ { $unwind: { path: "$utility" } } ] ).pretty()

The output looks quite large:

{
        "_id" : ObjectId("615dfce7f1ce6040485ecf62"),
        "utility" : "Videography",
        "onSale" : false,
        "name" : "RQ – Ariela Blingdom",
        "price" : 28000,
        "weight" : "3.4 kilograms",
        "additionalDetails" : {
                "material" : "polystyrene",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dfce7f1ce6040485ecf62"),
        "utility" : "Combat",
        "onSale" : false,
        "name" : "RQ – Ariela Blingdom",
        "price" : 28000,
        "weight" : "3.4 kilograms",
        "additionalDetails" : {
                "material" : "polystyrene",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dfce7f1ce6040485ecf62"),
        "utility" : "Rescue",
        "onSale" : false,
        "name" : "RQ – Ariela Blingdom",
        "price" : 28000,
        "weight" : "3.4 kilograms",
        "additionalDetails" : {
                "material" : "polystyrene",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dfce7f1ce6040485ecf62"),
        "utility" : "Construction",
        "onSale" : false,
        "name" : "RQ – Ariela Blingdom",
        "price" : 28000,
        "weight" : "3.4 kilograms",
        "additionalDetails" : {
                "material" : "polystyrene",
                "moreUses" : [
                        "Photography",
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dff74f1ce6040485ecf63"),
        "utility" : "Photography",
        "onSale" : true,
        "name" : "T5 – Doom Glancer",
        "price" : 8000,
        "weight" : "4 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dff74f1ce6040485ecf63"),
        "utility" : "Videography",
        "onSale" : true,
        "name" : "T5 – Doom Glancer",
        "price" : 8000,
        "weight" : "4 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dff74f1ce6040485ecf63"),
        "utility" : "Combat",
        "onSale" : true,
        "name" : "T5 – Doom Glancer",
        "price" : 8000,
        "weight" : "4 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dff74f1ce6040485ecf63"),
        "utility" : "Rescue",
        "onSale" : true,
        "name" : "T5 – Doom Glancer",
        "price" : 8000,
        "weight" : "4 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dff74f1ce6040485ecf63"),
        "utility" : "Construction",
        "onSale" : true,
        "name" : "T5 – Doom Glancer",
        "price" : 8000,
        "weight" : "4 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dff94f1ce6040485ecf64"),
        "utility" : "Photography",
        "onSale" : true,
        "name" : "TumblE – Jumper",
        "price" : 18000,
        "weight" : "5.6 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography"
                ]
        }
}
{
        "_id" : ObjectId("615dff94f1ce6040485ecf64"),
        "utility" : "Videography",
        "onSale" : true,
        "name" : "TumblE – Jumper",
        "price" : 18000,
        "weight" : "5.6 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography"
                ]
        }
}
{
        "_id" : ObjectId("615dff94f1ce6040485ecf64"),
        "utility" : "Combat",
        "onSale" : true,
        "name" : "TumblE – Jumper",
        "price" : 18000,
        "weight" : "5.6 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography"
                ]
        }
}
{
        "_id" : ObjectId("615dff94f1ce6040485ecf64"),
        "utility" : "Rescue",
        "onSale" : true,
        "name" : "TumblE – Jumper",
        "price" : 18000,
        "weight" : "5.6 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography"
                ]
        }
}
{
        "_id" : ObjectId("615dff94f1ce6040485ecf64"),
        "utility" : "Construction",
        "onSale" : true,
        "name" : "TumblE – Jumper",
        "price" : 18000,
        "weight" : "5.6 kilograms",
        "additionalDetails" : {
                "material" : "lithium",
                "moreUses" : [
                        "Videography"
                ]
        }
}
{
        "_id" : ObjectId("615dffb0f1ce6040485ecf65"),
        "utility" : "Videography",
        "onSale" : false,
        "name" : "Mount Hike Drone",
        "price" : 9000,
        "weight" : "7 kilograms",
        "additionalDetails" : {
                "material" : "aluminium",
                "moreUses" : [ ]
        }
}
{
        "_id" : ObjectId("615dffb0f1ce6040485ecf65"),
        "utility" : "Combat",
        "onSale" : false,
        "name" : "Mount Hike Drone",
        "price" : 9000,
        "weight" : "7 kilograms",
        "additionalDetails" : {
                "material" : "aluminium",
                "moreUses" : [ ]
        }
}
{
        "_id" : ObjectId("615dffb0f1ce6040485ecf65"),
        "utility" : "Rescue",
        "onSale" : false,
        "name" : "Mount Hike Drone",
        "price" : 9000,
        "weight" : "7 kilograms",
        "additionalDetails" : {
                "material" : "aluminium",
                "moreUses" : [ ]
        }
}
{
        "_id" : ObjectId("615dffb0f1ce6040485ecf65"),
        "utility" : "Construction",
        "onSale" : false,
        "name" : "Mount Hike Drone",
        "price" : 9000,
        "weight" : "7 kilograms",
        "additionalDetails" : {
                "material" : "aluminium",
                "moreUses" : [ ]
        }
}
{
        "_id" : ObjectId("615dffc8f1ce6040485ecf66"),
        "utility" : "Videography",
        "onSale" : false,
        "name" : "Defender Martial X55",
        "price" : 12000,
        "weight" : "9.1 kilograms",
        "additionalDetails" : {
                "material" : "alumnium",
                "moreUses" : [
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dffc8f1ce6040485ecf66"),
        "utility" : "Combat",
        "onSale" : false,
        "name" : "Defender Martial X55",
        "price" : 12000,
        "weight" : "9.1 kilograms",
        "additionalDetails" : {
                "material" : "alumnium",
                "moreUses" : [
                        "Security"
                ]
        }
}
Type "it" for more
> it
{
        "_id" : ObjectId("615dffc8f1ce6040485ecf66"),
        "utility" : "Rescue",
        "onSale" : false,
        "name" : "Defender Martial X55",
        "price" : 12000,
        "weight" : "9.1 kilograms",
        "additionalDetails" : {
                "material" : "alumnium",
                "moreUses" : [
                        "Security"
                ]
        }
}
{
        "_id" : ObjectId("615dffc8f1ce6040485ecf66"),
        "utility" : "Construction",
        "onSale" : false,
        "name" : "Defender Martial X55",
        "price" : 12000,
        "weight" : "9.1 kilograms",
        "additionalDetails" : {
                "material" : "alumnium",
                "moreUses" : [
                        "Security"
                ]
        }
}

As you can see, we have successfully deconstructed the utility array field using the $unwind operator.

Unwinding an Array Field Using the $unwind Operator with includeArrayIndex Option

Let use the includeArrayIndex option in this example.

> db.drones.aggregate( [
...   {
...     $unwind:
...       {
...         path: "$utility",
...         includeArrayIndex: "arrayNumber"
...       }
...    }])
  • Checking the output:
{ "_id" : ObjectId("615dfce7f1ce6040485ecf62"), "utility" : "Videography", "onSale" : false, "name" : "RQ – Ariela Blingdom", "price" : 28000, "weight" : "3.4 kilograms", "additionalDetails" : { "material" : "polystyrene", "moreUses" : [ "Photography", "Security" ] }, "arrayNumber" : NumberLong(0) }
{ "_id" : ObjectId("615dfce7f1ce6040485ecf62"), "utility" : "Combat", "onSale" : false, "name" : "RQ – Ariela Blingdom", "price" : 28000, "weight" : "3.4 kilograms", "additionalDetails" : { "material" : "polystyrene", "moreUses" : [ "Photography", "Security" ] }, "arrayNumber" : NumberLong(1) }
{ "_id" : ObjectId("615dfce7f1ce6040485ecf62"), "utility" : "Rescue", "onSale" : false, "name" : "RQ – Ariela Blingdom", "price" : 28000, "weight" : "3.4 kilograms", "additionalDetails" : { "material" : "polystyrene", "moreUses" : [ "Photography", "Security" ] }, "arrayNumber" : NumberLong(2) }
{ "_id" : ObjectId("615dfce7f1ce6040485ecf62"), "utility" : "Construction", "onSale" : false, "name" : "RQ – Ariela Blingdom", "price" : 28000, "weight" : "3.4 kilograms", "additionalDetails" : { "material" : "polystyrene", "moreUses" : [ "Photography", "Security" ] }, "arrayNumber" : NumberLong(3) }
{ "_id" : ObjectId("615dff74f1ce6040485ecf63"), "utility" : "Photography", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong(0) }
{ "_id" : ObjectId("615dff74f1ce6040485ecf63"), "utility" : "Videography", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong(1) }
{ "_id" : ObjectId("615dff74f1ce6040485ecf63"), "utility" : "Combat", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong(2) }
{ "_id" : ObjectId("615dff74f1ce6040485ecf63"), "utility" : "Rescue", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong(3) }
{ "_id" : ObjectId("615dff74f1ce6040485ecf63"), "utility" : "Construction", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong(4) }
{ "_id" : ObjectId("615dff94f1ce6040485ecf64"), "utility" : "Photography", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] }, "arrayNumber" : NumberLong(0) }
{ "_id" : ObjectId("615dff94f1ce6040485ecf64"), "utility" : "Videography", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] }, "arrayNumber" : NumberLong(1) }
{ "_id" : ObjectId("615dff94f1ce6040485ecf64"), "utility" : "Combat", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] }, "arrayNumber" : NumberLong(2) }
{ "_id" : ObjectId("615dff94f1ce6040485ecf64"), "utility" : "Rescue", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] }, "arrayNumber" : NumberLong(3) }
{ "_id" : ObjectId("615dff94f1ce6040485ecf64"), "utility" : "Construction", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] }, "arrayNumber" : NumberLong(4) }
{ "_id" : ObjectId("615dffb0f1ce6040485ecf65"), "utility" : "Videography", "onSale" : false, "name" : "Mount Hike Drone", "price" : 9000, "weight" : "7 kilograms", "additionalDetails" : { "material" : "aluminium", "moreUses" : [ ] }, "arrayNumber" : NumberLong(0) }
{ "_id" : ObjectId("615dffb0f1ce6040485ecf65"), "utility" : "Combat", "onSale" : false, "name" : "Mount Hike Drone", "price" : 9000, "weight" : "7 kilograms", "additionalDetails" : { "material" : "aluminium", "moreUses" : [ ] }, "arrayNumber" : NumberLong(1) }
{ "_id" : ObjectId("615dffb0f1ce6040485ecf65"), "utility" : "Rescue", "onSale" : false, "name" : "Mount Hike Drone", "price" : 9000, "weight" : "7 kilograms", "additionalDetails" : { "material" : "aluminium", "moreUses" : [ ] }, "arrayNumber" : NumberLong(2) }
{ "_id" : ObjectId("615dffb0f1ce6040485ecf65"), "utility" : "Construction", "onSale" : false, "name" : "Mount Hike Drone", "price" : 9000, "weight" : "7 kilograms", "additionalDetails" : { "material" : "aluminium", "moreUses" : [ ] }, "arrayNumber" : NumberLong(3) }
{ "_id" : ObjectId("615dffc8f1ce6040485ecf66"), "utility" : "Videography", "onSale" : false, "name" : "Defender Martial X55", "price" : 12000, "weight" : "9.1 kilograms", "additionalDetails" : { "material" : "alumnium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong(0) }
{ "_id" : ObjectId("615dffc8f1ce6040485ecf66"), "utility" : "Combat", "onSale" : false, "name" : "Defender Martial X55", "price" : 12000, "weight" : "9.1 kilograms", "additionalDetails" : { "material" : "alumnium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong(1) }
Type "it" for more
> it
{ "_id" : ObjectId("615dffc8f1ce6040485ecf66"), "utility" : "Rescue", "onSale" : false, "name" : "Defender Martial X55", "price" : 12000, "weight" : "9.1 kilograms", "additionalDetails" : { "material" : "alumnium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong(2) }
{ "_id" : ObjectId("615dffc8f1ce6040485ecf66"), "utility" : "Construction", "onSale" : false, "name" : "Defender Martial X55", "price" : 12000, "weight" : "9.1 kilograms", "additionalDetails" : { "material" : "alumnium", "moreUses" : [ "Security" ] }, "arrayNumber" : NumberLong(3) }

You might have noticed the “arrayNumber” field that we set in the expression. This denotes the array index number.

Unwinding an Array Field Using the $unwind Operator with preserveNullAndEmptyArrays Option

We will use the preserveNullAndEmptyArrays in this example for the $unwind operator:

> db.drones.aggregate( [
...    { $unwind: { path: "$utility", preserveNullAndEmptyArrays: true } }
... ] )
  • Checking our database again:
{ "_id" : ObjectId("615dfce7f1ce6040485ecf62"), "utility" : "Videography", "onSale" : false, "name" : "RQ – Ariela Blingdom", "price" : 28000, "weight" : "3.4 kilograms", "additionalDetails" : { "material" : "polystyrene", "moreUses" : [ "Photography", "Security" ] } }
{ "_id" : ObjectId("615dfce7f1ce6040485ecf62"), "utility" : "Combat", "onSale" : false, "name" : "RQ – Ariela Blingdom", "price" : 28000, "weight" : "3.4 kilograms", "additionalDetails" : { "material" : "polystyrene", "moreUses" : [ "Photography", "Security" ] } }
{ "_id" : ObjectId("615dfce7f1ce6040485ecf62"), "utility" : "Rescue", "onSale" : false, "name" : "RQ – Ariela Blingdom", "price" : 28000, "weight" : "3.4 kilograms", "additionalDetails" : { "material" : "polystyrene", "moreUses" : [ "Photography", "Security" ] } }
{ "_id" : ObjectId("615dfce7f1ce6040485ecf62"), "utility" : "Construction", "onSale" : false, "name" : "RQ – Ariela Blingdom", "price" : 28000, "weight" : "3.4 kilograms", "additionalDetails" : { "material" : "polystyrene", "moreUses" : [ "Photography", "Security" ] } }
{ "_id" : ObjectId("615dff74f1ce6040485ecf63"), "utility" : "Photography", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] } }
{ "_id" : ObjectId("615dff74f1ce6040485ecf63"), "utility" : "Videography", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] } }
{ "_id" : ObjectId("615dff74f1ce6040485ecf63"), "utility" : "Combat", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] } }
{ "_id" : ObjectId("615dff74f1ce6040485ecf63"), "utility" : "Rescue", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] } }
{ "_id" : ObjectId("615dff74f1ce6040485ecf63"), "utility" : "Construction", "onSale" : true, "name" : "T5 – Doom Glancer", "price" : 8000, "weight" : "4 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Security" ] } }
{ "_id" : ObjectId("615dff94f1ce6040485ecf64"), "utility" : "Photography", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] } }
{ "_id" : ObjectId("615dff94f1ce6040485ecf64"), "utility" : "Videography", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] } }
{ "_id" : ObjectId("615dff94f1ce6040485ecf64"), "utility" : "Combat", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] } }
{ "_id" : ObjectId("615dff94f1ce6040485ecf64"), "utility" : "Rescue", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] } }
{ "_id" : ObjectId("615dff94f1ce6040485ecf64"), "utility" : "Construction", "onSale" : true, "name" : "TumblE – Jumper", "price" : 18000, "weight" : "5.6 kilograms", "additionalDetails" : { "material" : "lithium", "moreUses" : [ "Videography" ] } }
{ "_id" : ObjectId("615dffb0f1ce6040485ecf65"), "utility" : "Videography", "onSale" : false, "name" : "Mount Hike Drone", "price" : 9000, "weight" : "7 kilograms", "additionalDetails" : { "material" : "aluminium", "moreUses" : [ ] } }
{ "_id" : ObjectId("615dffb0f1ce6040485ecf65"), "utility" : "Combat", "onSale" : false, "name" : "Mount Hike Drone", "price" : 9000, "weight" : "7 kilograms", "additionalDetails" : { "material" : "aluminium", "moreUses" : [ ] } }
{ "_id" : ObjectId("615dffb0f1ce6040485ecf65"), "utility" : "Rescue", "onSale" : false, "name" : "Mount Hike Drone", "price" : 9000, "weight" : "7 kilograms", "additionalDetails" : { "material" : "aluminium", "moreUses" : [ ] } }
{ "_id" : ObjectId("615dffb0f1ce6040485ecf65"), "utility" : "Construction", "onSale" : false, "name" : "Mount Hike Drone", "price" : 9000, "weight" : "7 kilograms", "additionalDetails" : { "material" : "aluminium", "moreUses" : [ ] } }
{ "_id" : ObjectId("615dffc8f1ce6040485ecf66"), "utility" : "Videography", "onSale" : false, "name" : "Defender Martial X55", "price" : 12000, "weight" : "9.1 kilograms", "additionalDetails" : { "material" : "alumnium", "moreUses" : [ "Security" ] } }
{ "_id" : ObjectId("615dffc8f1ce6040485ecf66"), "utility" : "Combat", "onSale" : false, "name" : "Defender Martial X55", "price" : 12000, "weight" : "9.1 kilograms", "additionalDetails" : { "material" : "alumnium", "moreUses" : [ "Security" ] } }

Perfect! You might have noticed how some empty arrays are preserved by the $unwind operator option in this example.

Read More: Ultimate Guide to Using addToSet Operator in MongoDB with Mongoose

Conclusion

I hope that you learned to use the $unwind operator in MongoDB with our simple tutorial. We’re expanding our tutorial collection and will continue to do so. Let us know what you’d like to see next.

References

MongoDB

Aneesha S
Aneesha S
Articles: 172