How to add filters such as _limit to strapi controller (findOne)?

When I fist http://localhost:1338/course-series/2 I get

  name: 'example two',
  courses: [/*array of data*/],

I wanted to get just the courses, so I change the course-series/controllers/course-series.js to:

const { sanitizeEntity } = require("strapi-utils");
module.exports = {
    async findOne(ctx) {
        const { id } = ctx.params
        const entity = await["course-series"].findOne({id});
        return sanitizeEntity(, { model: strapi.models["course-series"] });

It works exactly how I wanted, I just get the courses. But I also want to be able to do this:


So instead of getting all the courses, I want to get 2. With the above code, it doesn’t work. What is missing?

You can extending the core controller for api, add your filter or conditions in it. Here is docs: Backend customization - Controllers - Strapi Developer Docs

This question is just like yours: How to set Limit of a relation - Discussions - Strapi Community Forum

I did this

async find(ctx) { 
    let entities,
        filters = convertRestQueryParams(ctx.request.query); 
      entities = await["book-series"].find({}); // getting all, ignoring the query
    return => {
        entity.books = entity.books.slice(filters.start, filters.limit)
        return sanitizeEntity(entity, { model: strapi.models["book-series"] });

.slice(filters.start, filters.limit) did the trick. It works. Is there any downside to doing this? Would it be fast enough when I have 100s of entries or would the slicing slow things down?

@My_Marginalia I think it’s not a good idea to get all store in a param entities.
I will update a case late when I can.

1 Like

I don’t know if this api is your customization, I assume that you have two collection, course-series 1–>N courses, then we can add controller extension on api::course-serial.course-serial

const { createCoreController } = require('@strapi/strapi').factories;

module.exports = createCoreController('api::course-serial.course-serial',
  ({ strapi }) => ({
    async findOne(ctx) {
      // it will be better to set default
      const { _start = 0, _limit = 3 } = ctx.request.query

      const response = super.findOne(ctx).then(
        res =>, _limit)

      return response