| by Arround The Web | No comments

How to Manipulate the Arrays with MongoDB Unwind

In MongoDB, the aggregate $unwind operator is employed to break an array column into distinct records for every component in the array. To state it another way, it breaks down an array column from the input records to output a record for every component. In this guide, we’ll discuss the uses of MongoDB’s $unwind aggregation operator with the help of simple MongoDB code examples.

Create a Collection

Starting with the use of collections in MongoDB, we utilize the “show collections” functions to display the existing ones. We found zero collections at the moment. Therefore, we generate a new collection with the title “Detail” via the createCollection() method. MongoDB displays the existence of this collection.

test> show collections

test> db.createCollection("Detail")

test> show collections

Insert the Records

To work with the “unwind aggregation” function of MongoDB, we must have at least one field of array type. To do so, we add a total of two records in the “Detail” collection with the “sprice” and “brand” fields. The “brand” field from each record contains three values in the array format. The “sprice” field displays the starting price of each brand that is listed in the “brand” field array-type. The acknowledgment message confirms the addition of records.

test> db.Detail.insertMany([ { sprice: 10000, brand: ["flormar", "Muicin", "Christine"] },

... { sprice: 15000, brand: ["HudaBeauty", "Nars", "Charlotte Telburry"] } ])

Display the Records

The find() method of MongoDB is used in the “db” command to display all the inserted records of the “Detail” collection. The output displays the unique identifier field “-id” and the sprice field to show the starting prices of the respective brands that are mentioned in the “brand” field in an array format.

test> db.Detail.find({})

Example 1: Unwind Aggregation on Array Fields

To understand the $unwind aggregation in MongoDB, we generate the following code example. The unwind aggregation is used by the MongoDB aggregation function to handle the “Detail” collection within the aggregate() function query.

To generate a distinct record for every component of an array column in the records, the $unwind step is utilized. It is applied to the “brand” field as per the query that is mentioned in the attached code. The output of this code displays a total of six records that are generated from the original two records of the “Detail” collection.

The output generates the “_id:” field for the distinctive identification of all the six records that are produced after using the $unwind aggregation, i.e. each record has its unique identifier number as “_id”. The second field for output records is the “sprice” which displays the value of 1000 for the first three records and the value of 15000 for the next three records. This is because the original two records are splitted into six using the brand names while the first three brands have the same “Sprice” field value, and the rest have the same.

The third field which is “brand” of each record from the output of six documents shows the brand names separately using the “brand” array field from the original two records in the “Detail” collection. The values of the “brand” field from the first record of the “Detail” collection are splitted into the first three records of the output.

While the values of the “brand” field from the second record of the “Detail” collection are divided into the last three displayed records, and the “sprice” field is added to each one separately. The pretty() function is applied to the result to display the more human-readable format of the output.

test> db.Detail.aggregate([ { $unwind : "$brand" } ]). pretty()

Example 2: Unwind Aggregation on Non-Array Fields

Earlier to the MongoDB 3.2 version, an exception occurs if a non-array column is specified as the value to the $unwind function. Starting with MongoDB 3.2 and the newest versions, any non-array column that gets an error using a missing, null, or blank array is handled as a separate component array. Therefore, we consider a simple MongoDB command code to demonstrate it.

We use the unwind operator on the “sprice” field of the “Detail” collection via the aggregate function, although the “sprice” field is not an array-type column. The output for this unwind command script displays the same result as we have in displaying the “Detail” collection records via the “find” function in the previous illustrations, i.e. no exception occurred at the time of execution.

test> db.Detail.aggregate([ { $unwind : "$sprice" } ]). pretty()

Example 3: Unwind Aggregation on Null and Empty Array Fields

Let’s say we have a “Data” collection in the MongoDB, and we add a total of five records in it as shown in the following illustration. The output of the “find” function that is used on the “Data” collection displays that each record has the “name” and “score” fields. While the “name” field contains the non-array values, the “score” field contains an array as a value in some records. The first record contains two values in the “score” field as an array, while the “score” field of the second record is initialized to null.

Just like that, the third record has no “score” field, while the fourth record has a non-array “score” field. The last record contains the empty [] array-type “score” field.

test> db.Data.find({})

Now, we use the unwind aggregation on the null and empty array fields to see how it works on these. The unwind aggregation is applied to the “score” field as shown from the query. After the execution of the shown query, we get three records in return at our MongoDB output shell. The first two records are generated from the splitting of the first record of the original collection using the unwind aggregation which contains an array-type “score” field with two values.

At the same time, the fourth record from the original collection is displayed as it is in the output as the third record. For the rest, the null and empty records of the original collection are ignored by the unwind aggregation.

test> db.Data.aggregate([ { $unwind : "$score" } ]).pretty()

Conclusion

We demonstrated how an unwind aggregation of MongoDB can be applied to the array fields of the MongoDB collection to produce independent records. For this, we successfully added the MongoDB code examples to show how it works on array, non-array, null, and empty fields.

Share Button

Source: linuxhint.com

Leave a Reply