Open In App

Mongoose Query prototype.slice() API

Last Updated : 07 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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([path]).slice(field, val)

Or

Modal.find().slice([field], val)

parameters:

  • path: path is a string that represents the field name, that is going to be searched, this is a must-have parameter.
  • field: field is a string that represents the field name that is going to be sliced.
  • val: It is a number or an array of two numbers that represent the range. If this is a number then the range will be started from 0.

Installation of mongoose module:

Step 1: You can visit the link to Install the mongoose module. You can install this package by using this command.

npm install mongoose

Step 2: After installing the mongoose module, you can check your mongoose version in the command prompt using the command.

npm version mongoose

Step 3: After that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command.

node index.js

Project Structure: The project structure will look like this.

 

Example 1: We have some customer data that contains their name, interest, and orderCount. The interest contains an array of multiple games. We want to get all the elements with their interest sliced into 3 elements.

index.js

Javascript




// Require mongoose module
const mongoose = require('mongoose');
  
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true
})
  
// Defining customerSchema schema
const customerSchema = new mongoose.Schema(
    { name: String, interest: Array, orderCount: Number }
)
  
// Defining customerSchema model
const Customer = mongoose.model(
    'Customer', customerSchema);
  
Customer.find().slice("interest", 3).then((res) => {
    console.log(res)
}).catch((err) => {
    console.log(err)
});


Steps to run the application:

Step 1:  Make sure you have installed the mongoose module using the following command:

npm install mongoose

Step 2: Below is the sample data in the database before the function is executed, You can use any GUI tool or terminal to see the database like we have used the MongoDB compass GUI tool as shown below:

 

Step 3: Run the index.js file using the below command:

node index.js

Output:

 

Example 2: In this example, we are slicing the elements in a range.

index.js

Javascript




// Require mongoose module
const mongoose = require('mongoose');
  
// Set Up the Database connection
mongoose.connect(
    useNewUrlParser: true,
    useUnifiedTopology: true
})
  
// Defining customerSchema schema
const customerSchema = new mongoose.Schema(
    { name: String, interest: Array, orderCount: Number }
)
  
// Defining customerSchema model
const Customer = mongoose.model(
    'Customer', customerSchema);
  
Customer.find().slice("interest", [2, 4]).then((res) => {
    console.log(res)
}).catch((err) => {
    console.log(err)
});


Step to run the application: Run the index.js file using the below command:

node index.js

Output:

 

References: https://mongoosejs.com/docs/api/query.html#query_Query-slice



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads