Mongoose Query prototype.skip() API
Last Updated :
26 Apr, 2025
Mongoose is an Object Data Modeling (ODM) library for MongoDB. It defines a strongly-typed schema, with default values and schema validations which are later mapped to a MongoDB document.
The Mongoose Query API skip method is used to determine how many documents to skip after applying the Mongoose Query. Let's understand more about this with some examples.
Syntax:
Query.prototype.skip()
Parameters: It takes one parameter.
- val: It is a number that determines how many documents to skip
Return type: It returns the Query object.
Creating node application And Installing Mongoose:
Step 1: Create a node application using the following command:
mkdir folder_name
cd folder_name
npm init -y
touch main.js
Step 2: After creating the ReactJS application, Install the required module using the following command
npm install mongoose
Project Structure: It will look like the following.
GUI Representation of the Database using MongoDB Compass: Currently, the collection has no data.
Example 1: In this example, we will use the Query API skip() method to skip one document after the query is applied
Filename: main.js
JavaScript
const mongoose = require('mongoose')
// Database connection
mongoose.connect(
'mongodb://localhost:27017/query-helpers',
{
dbName: 'event_db',
useNewUrlParser: true,
useUnifiedTopology: true
}, err => err ? console.log(err)
: console.log('Connected to database'));
const personSchema = new mongoose.Schema({
name: {
type: String,
},
age: {
type: Number,
}
});
const personsArray = [
{
name: 'Luffy',
age: 22
},
{
name: 'Nami',
age: 30
},
{
name: 'Zoro',
age: 15
}
]
const Person = mongoose.model('Person', personSchema);
(async () => {
await Person.insertMany(personsArray);
const query = Person.find()
const persons = await query.skip(1)
console.log(persons);
})()
Step to Run Application: Run the application using the following command from the root directory of the project:
node main.js
Output: We see that the value remains unchanged in the result.
GUI Representation of the Database using MongoDB Compass:
Example 2: In this example, we will use the Query API skip() method to skip one document after the query is applied, and then limit the number of documents to one.
Filename: main.js
JavaScript
const mongoose = require('mongoose')
// Database connection
mongoose.connect('mongodb://localhost:27017/query-helpers', {
dbName: 'event_db',
useNewUrlParser: true,
useUnifiedTopology: true
}, err => err ? console.log(err)
: console.log('Connected to database'));
const personSchema = new mongoose.Schema({
name: {
type: String,
},
age: {
type: Number,
}
});
const personsArray = [
{
name: 'Luffy',
age: 22
},
{
name: 'Nami',
age: 30
},
{
name: 'Zoro',
age: 15
}
]
const Person = mongoose.model('Person', personSchema);
(async () => {
await Person.insertMany(personsArray);
const query = Person.find()
const persons = await query.skip(1).limit(1)
console.log(persons);
})()
Step to Run Application: Run the application using the following command from the root directory of the project:
node main.js
Output: We see that the value remains unchanged in the result.
GUI Representation of the Database using MongoDB Compass:
Reference: https://mongoosejs.com/docs/api/query.html#query_Query-skip
Similar Reads
Mongoose Query prototype.size() API
Mongoose is an Object Data Modeling (ODM) library for MongoDB. It defines a strongly-typed schema, with default values and schema validations which are later mapped to a MongoDB document. The size( ) method of mongoose Query API is used to find the documents that have a field that contains an array
3 min read
Mongoose Query prototype.slice() API
Mongoose is an Object Data Modeling (ODM) library for MongoDB. It defines a strongly-typed schema, with default values and schema validations which are later mapped to a MongoDB document. The slice method is used to limit the number of elements returned by the query. Syntax: Modal.find().where([pat
3 min read
Mongoose Query.prototype.set() API
The Mongoose Query API set() method is used to add a $set middleware that helps in updating one or more than one MongoDB document depending on the path specified and the value passed in the argument. Syntax: Query.prototype.set(path, val) Parameters: It accepts the following parameters as mentioned
3 min read
Mongoose Query.prototype.w() API
The Mongoose Query API.prototype.w() method of the Mongoose API is used on the Query objects. It allows us to set the specified number of MongoDB servers that must acknowledge the write operations. before the write event is successfully executed. Providing "1" as a parameter to the method indicates
3 min read
Mongoose Query prototype.sort() API
Mongoose is an Object Data Modeling (ODM) library for MongoDB. It defines a strongly-typed schema, with default values and schema validations which are later mapped to a MongoDB document. The Mongoose Query API sort() method is used to sort out the documents, from a collection, using the MongoDB qu
3 min read
Mongoose Query.prototype.read() API
The Mongoose Query API.prototype.read() method of the Mongoose API is used on the Query objects. It allows us to tell the MongoDB from which node it should read the data. Using this method we can set one out of five different preferences to let MongoDB read the data. Let us understand the read() met
3 min read
Mongoose Query.prototype.lt() API
The Mongoose Query API lt() method is used to scan all the documents in a collection and return those whose path values are less than that passed in the method's argument. Syntax: Query.prototype.lt( path, val ) Parameters: It accepts the following parameters as mentioned above and described below:
3 min read
Mongoose Query.prototype.ne() API
The Mongoose Query API.prototype.ne() method of the Mongoose API is used on the Query objects. It allows us to extract the documents where the value of the given field is not equal to the specified values for the field. Let us understand ne() method using an example. Syntax: query.ne( path, value );
3 min read
Mongoose Query.prototype.ne() API
The Mongoose Query API.prototype.ne() method of the Mongoose API is used on the Query objects. It allows us to extract the documents where the value of the given field is not equal to the specified values for the field. Let us understand ne() method using an example. Syntax: query.ne( path, value );
3 min read
Mongoose Query.prototype.gt() API
The Mongoose Query API gt() method is used to scan all the documents in a collection and return those whose path values are greater than that passed in the method's argument. Syntax: Query.prototype.gt( path, val ) Parameters: It accepts the following parameters as mentioned above and described belo
3 min read