Mongoose Aggregate.prototype.skip() API
Last Updated :
26 Apr, 2025
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
const mongoose = require( "mongoose" );
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
const mongoose = require( "mongoose" );
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
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line providing more flexibility. HTML adds Structure to a web page, CSS st
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook created React. Developers with a Javascript background can easily develop web applications
15+ min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
Backpropagation in Neural Network
Backpropagation is also known as "Backward Propagation of Errors" and it is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network. In this article we will explore what
10 min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. It is essential for both front-end and back-end developers to have a strong command of
15+ min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read