Open In App

Mongoose Aggregate prototype.unwind() API

Improve
Improve
Like Article
Like
Save
Share
Report

The Aggregate API.prototype.unwind() method of the Mongoose API is used to perform aggregation tasks. It allows us to break down complex and nested array or document fields in the MongoDB database or Schema. It returns a document object for every element present in the field. It deconstructs the array attribute to output a document object for every element in an array.

Syntax:

aggregate.unwind(field)

Parameters: This method accepts a single parameter as mentioned above and described below:

  • field: It is the field or attribute in the MongoDB database.

Return Value: This method returns a document object for every element in the array field.

Setting up Node.js application:

Step 1: Create a Node.js application using the following command:

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command:

npm install mongoose

Project Structure: The project structure will look like this: 

 

Database Structure: The database structure will look like this, the following documents are present in the collection.

 

Example 1: In this example, we have established a database connection using mongoose and defined model over studentSchema, having three columns or fields “studentId”, “studentName”, and “marks”. At the end, we are using unwind() method on marks field to deconstruct the array and to get document for every element in array as output.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const studentSchema = new mongoose.Schema({
    studentId: Number,
    studentName: String,
    marks: []
});
  
const Student = mongoose.model('Student', studentSchema);
  
const aggregate = Student.aggregate();
aggregate.unwind("$marks").exec((err, result) => {
    if (err) {
        console.log(err)
    } else {
        console.log(result)
    }
})


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

[
  {
    _id: new ObjectId("63862e73d408a33b4481fd05"),
    studentId: 1,
    studentName: 'Aakash',
    marks: 50,
    __v: 0
  },
  {
    _id: new ObjectId("63862e73d408a33b4481fd05"),
    studentId: 1,
    studentName: 'Aakash',
    marks: 65,
    __v: 0
  },
  {
    _id: new ObjectId("63862e73d408a33b4481fd05"),
    studentId: 1,
    studentName: 'Aakash',
    marks: 75,
    __v: 0
  },
  {
    _id: new ObjectId("63862e73d408a33b4481fd06"),
    studentId: 2,
    studentName: 'Bhavesh',
    marks: 80,
    __v: 0
  },
  {
    _id: new ObjectId("63862e73d408a33b4481fd06"),
    studentId: 2,
    studentName: 'Bhavesh',
    marks: 70,
    __v: 0
  },
  {
    _id: new ObjectId("63862e73d408a33b4481fd06"),
    studentId: 2,
    studentName: 'Bhavesh',
    marks: 90,
    __v: 0
  }
]

Example 2: In this example, we have established a database connection using mongoose and defined model over statesSchema, having three columns or fields “stateId”, “stateName”, and “city”. At the end, we are using unwind() method on city field to deconstruct the array and to get document for every element in array as output. In the output we can see we are getting document object for each city that is present in city array at schema level.

Database Structure: The database structure will look like this, the following documents are present in the collection.

 

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const statesSchema = new mongoose.Schema({
    stateId: Number,
    stateName: String,
    city: []
});
  
const State = mongoose.model('State', statesSchema);
  
const aggregate = State.aggregate();
  
aggregate.unwind("$city").then(result => {
    console.log(result)
}).catch(err => {
    console.log(err)
})


Step to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

[
  {
    _id: new ObjectId("63863213430c4520ce61c186"),
    stateId: 1,
    stateName: 'Maharashtra',
    city: 'Mumbai',
    __v: 0
  },
  {
    _id: new ObjectId("63863213430c4520ce61c186"),
    stateId: 1,
    stateName: 'Maharashtra',
    city: 'Pune',
    __v: 0
  },
  {
    _id: new ObjectId("63863213430c4520ce61c186"),
    stateId: 1,
    stateName: 'Maharashtra',
    city: 'Nagpur',
    __v: 0
  },
  {
    _id: new ObjectId("63863213430c4520ce61c187"),
    stateId: 2,
    stateName: 'Madhya Pradesh',
    city: 'Bhopal',
    __v: 0
  },
  {
    _id: new ObjectId("63863213430c4520ce61c187"),
    stateId: 2,
    stateName: 'Madhya Pradesh',
    city: 'Indore',
    __v: 0
  }
]

Reference: https://mongoosejs.com/docs/api/aggregate.html#aggregate_Aggregate-unwind



Last Updated : 05 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads