Using populate with limit in REST API

System Information
  • Strapi Version: 4.1.9
  • Operating System: Endevour OS (Arch Linux)
  • Database: default in dev env
  • Node Version: 14.19.1
  • NPM Version: 6.14.16
  • Yarn Version: 1.22.18

Hi,

I have two collections, let’s say collection A and collection B. They have a one to many relation: A items can have many B items.

I can make a call to the api for getting items in A populated with related items in B by calling:

http://localhost:1337/api/a?populate[b][populate]=*

that works great. I would like to get only one item from B ordering by a date field, this is, instead of getting all B items related to the A item, get only one sorted by a date field.

I’ve tried sorting, like this:

http://localhost:1337/api/a?populate[b][sort][0]=startDate:desc

and works great, but I would like to add a limit for getting only one item.

Any idea about doing this through the generated endpoint or do I have to make a custom endpoint for this?

Thanks!

It’s currently not possilbe to limit/paginate populated relations and I don’t believe we plan to implement any logic on this as the way we would build the SQL queries would mean basically executing another SQL query for every entity you are populating which could be hundreds of queries.

Generally you shouldn’t be doing all of this in a single API request and instead you should call the endpoint for that relational data directly (with filters) and paginate that way.

1 Like