Open In App

Mongoose Aggregate.prototype.skip() API

Last Updated : 06 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Aggregate API.prototype.skip() method of the Mongoose API is used to perform aggregation tasks. It allows us to skip a specified number of document objects from the collection and pass the remaining documents to the next stage of the pipeline and result set.

Syntax:

aggregate().skip( number )

Parameters: This method accepts a single parameter as discussed below:

  • number: It is used to specify the number of documents to be skipped.

Returns: This method returns the result set in the form of an array.

Setting up Node.js Mongoose Module:

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 userSchema, having two columns or fields  “name”, and “bornYear”. At the end, we are calling skip() and passing “5” as an argument to skip first 5 documents in collection and get the remaining 3 documents in the result set.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    bornYear: Number
});
  
const User = mongoose.model('User', userSchema);
  
User.aggregate().skip(5)
    .then((successCb, errorCb) => {
        if (successCb) {
            console.log(successCb);
        } else {
            console.log(errorCb);
        }
    })


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("6387aa99c92df30f995e830e"),
    name: 'Takshwi',
    bornYear: 2018,
    __v: 0
  },
  {
    _id: new ObjectId("6387aa99c92df30f995e830f"),
    name: 'Kashwi',
    bornYear: 2021,
    __v: 0
  },
  {
    _id: new ObjectId("6387aa99c92df30f995e8310"),
    name: 'Kinjal',
    bornYear: 2021,
    __v: 0
  }
]

Example 2: In this example, we have established a database connection using mongoose and defined model over userSchema, having two columns or fields  “name”, and “bornYear”. At the end, we are calling aggregate() on User model and passing pipelined object as an array to skip first 7 records out of 8 records. In the output, we are getting single and last document from the collection after skipping first 7 documents.

Filename: app.js

Javascript




// Require mongoose module
const mongoose = require("mongoose");
  
// Set Up the Database connection
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
  
const userSchema = new mongoose.Schema({
    name: String,
    bornYear: Number
});
  
const User = mongoose.model('User', userSchema);
  
User.aggregate([{ $skip: 7 }])
    .exec((error, success) => {
        if (error) {
            console.log(error);
        } else {
            console.log(success);
        }
    })


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("6387aa99c92df30f995e8310"),
    name: 'Kinjal',
    bornYear: 2021,
    __v: 0
  }
]

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads